我在向量中存储一些结构。在一个循环中,我试图处理一些输入,在每次迭代中,我都希望将一个新的项插入到向量中,或者改变现有元素(取决于它是否已经在向量中)。
所以我想做这样的事。
struct Foo {
id: char,
data: i32,
}
fn main() {
let mut cache: Vec<Foo> = Vec::new();
for a in vec!['c'] {
let foo = cache.iter_mut().filter(|s| s.id == a).nth(0);
match foo {
Some(f) => {
f.data = f.data + 1;
}
None => {
cache.push(Foo { id: a, data: 0 });
}
}
}
}
我有个错误
None
臂说
error[E0499]: cannot borrow `cache` as mutable more than once at a time
--> src/main.rs:17:17
|
10 | let foo = cache.iter_mut().filter(|s| s.id == a).nth(0);
| ----- first mutable borrow occurs here
...
17 | cache.push(Foo { id: a, data: 0 });
| ^^^^^ second mutable borrow occurs here
...
20 | }
| - first borrow ends here
避免这个问题的惯用方法是什么?我尝试了下面的解决方法,看起来很有效,但感觉很笨拙。
for a in vec!['c'] {
let mut found = false;
{
let step = cache.iter_mut().filter(|s| s.id == a).nth(0);
match step {
Some(f) => {
f.data = f.data + 1;
found = true;
}
None => (),
}
}
if !found {
cache.push(Foo { id: a, data: 0 });
}
}
有更简单的解决办法吗?