我有两个具有相同键的HashMap<&str, String>
,并且我希望创建一个具有相同键的HashMap
,将值组合在一起。我不想保留对前两个HashMap
的引用,但想将String
移到新的HashMap
。
use std::collections::HashMap;
#[derive(Debug)]
struct Contact {
phone: String,
address: String,
}
fn main() {
let mut phones: HashMap<&str, String> = HashMap::new();
phones.insert("Daniel", "798-1364".into());
phones.insert("Ashley", "645-7689".into());
phones.insert("Katie", "435-8291".into());
phones.insert("Robert", "956-1745".into());
let mut addresses: HashMap<&str, String> = HashMap::new();
addresses.insert("Daniel", "12 A Street".into());
addresses.insert("Ashley", "12 B Street".into());
addresses.insert("Katie", "12 C Street".into());
addresses.insert("Robert", "12 D Street".into());
let contacts: HashMap<&str, Contact> = phones.keys().fold(HashMap::new(), |mut acc, value| {
acc.entry(value).or_insert(Contact {
phone: *phones.get(value).unwrap(),
address: *addresses.get(value).unwrap(),
});
acc
});
println!("{:?}", contacts);
}
但是我有一个错误
error[E0507]: cannot move out of a shared reference
--> src/main.rs:24:20
|
24 | phone: *phones.get(value).unwrap(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `std::string::String`, which does not implement the `Copy` trait
error[E0507]: cannot move out of a shared reference
--> src/main.rs:25:22
|
25 | address: *addresses.get(value).unwrap(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `std::string::String`, which does not implement the `Copy` trait
答案 0 :(得分:4)
HashMap::get
返回一个Option<&V>
,即对映射内值的引用。除非*
实现V
,否则您不能移出Copy
的引用。您需要使用另一种方法将值移出映射,即HashMap::remove
(请注意,它返回Option<V>
)。
如果尝试使用remove
重写相同的算法,则会收到不同的错误:
let contacts: HashMap<&str, Contact> = phones.keys().fold(HashMap::new(), |mut acc, value| {
acc.entry(value).or_insert(Contact {
phone: phones.remove(value).unwrap(),
address: addresses.remove(value).unwrap(),
});
acc
});
error[E0502]: cannot borrow `phones` as mutable because it is also borrowed as immutable
--> src/main.rs:22:79
|
22 | let contacts: HashMap<&str, Contact> = phones.keys().fold(HashMap::new(), |mut acc, value| {
| ------ ---- ^^^^^^^^^^^^^^^^ mutable borrow occurs here
| | |
| | immutable borrow later used by call
| immutable borrow occurs here
23 | acc.entry(value).or_insert(Contact {
24 | phone: phones.remove(value).unwrap(),
| ------ second borrow occurs due to use of `phones` in closure
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.
此错误告诉您在对数据结构进行迭代时不能对其进行变异,因为对数据结构进行变异可能会使迭代器无效。 Sometimes you can solve this with interior mutability,但在这种情况下,您无需执行任何操作。迭代时,只需致电phones.into_iter()
,即可将电话号码移出地图。然后,很容易使用map
创建(&str, Contact)
元组,最后collect
将它们全部重新组合成HashMap
。
let contacts: HashMap<_, _> = phones
.into_iter()
.map(|(key, phone)| {
(
key,
Contact {
phone,
address: addresses.remove(key).unwrap(),
},
)
})
.collect();
答案 1 :(得分:1)
zip
是您的朋友。但是这里的“业务逻辑”规定它仅适用于排序的映射。因此,如果可以使用BTreeMap
代替HashMap
,则可以使用以下方法:
fn main() {
let mut phones: BTreeMap<&str, String> = BTreeMap::new();
...
let mut addresses: BTreeMap<&str, String> = BTreeMap::new();
...
let contacts: BTreeMap<&str, Contact> = phones
.into_iter()
.zip(addresses.into_iter())
.map(|((name, phone), (_, addr))| {
(
name,
Contact {
phone: phone,
address: addr,
},
)
})
.collect();
println!("{:#?}", contacts);
}