【发布时间】:2017-05-22 18:59:23
【问题描述】:
use std::collections::{HashMap, HashSet};
use std::hash::{Hash};
fn test(data: &mut HashMap<String, HashSet<String>>) {
match data.get("foo") {
None => return,
Some(xs) => {
let xs: Vec<String> = xs.iter().map(|x| x.to_owned()).collect();
// How to drop `data` here so that I can borrow `data`.
for x in xs {
// Mutable borrow occurs, because previous `data` is still in scope.
data.remove(&x);
}
}
}
}
上面的代码不起作用,因为我再次可变借用了data,而之前的借用仍在范围内。但是,我找不到一个简单的方法来解除之前借用的绑定。
另外,有没有更好的方法来复制xs,以便我可以在迭代时修改哈希图。
【问题讨论】:
标签: rust