我有一个 String
atp.Status="draft";pureMM.maxOccurs="1";pureMM.minOccurs="1";xml.attribute="true"
,想将其拆分两次并将其插入到 HashMap 中。对于第一次分割,我使用 ;
,对于第二次分割,我使用 =
。它应该看起来像这样。
appinfo {
atp.Status: "draft",
pureMM.maxOccurs: "1",
pureMM.minOccurs: "1",
xml.attribute: "true",
}
这是我的代码:
let mut appinfo: HashMap<String,String> = HashMap::new();
annotation.text = atp.Status="draft";pureMM.maxOccurs="1";pureMM.minOccurs="1";xml.attribute="true".to_string();
appinfo = HashMap::from_iter(annotation.text.unwrap().split(";").map(|a|a.split_once("=").unwrap()));
我得到:
= note: expected struct `HashMap<String, String, RandomState>`
found struct `HashMap<&str, &str, _>`
我已经使用
to_string()
、 into_iter()
或 collect()
尝试了地图内和外部所有可能的变体和组合。我唯一有效的解决方案是将 HashMap
的类型更改为 <&str, &str>
但我不希望这样。
如何将两根弦拼接成正常的弦?
这真的很微不足道,只是
to_string()
split_once
的两个部分:
let appinfo = HashMap::from_iter(annotation.text.unwrap().split(";").map(|a| {
let (first, last) = a.split_once("=").unwrap();
(first.to_string(), last.to_string())
}));