借用 self 时在 HashMap 上进行 Rust 循环
Rust loop on HashMap while borrowing self
我有一个 Element
结构,它实现了一个更新方法,该方法需要一个滴答持续时间。该结构包含一个组件向量。允许这些组件在更新时修改元素。我在这里遇到借用错误,我不确定该怎么做。我尝试用块修复它,但块似乎不满足借用检查器的要求。
use std::collections::HashMap;
use std::time::Duration;
pub struct Element {
pub id: String,
components: HashMap<String, Box<Component>>,
}
pub trait Component {
fn name(&self) -> String;
fn update(&mut self, &mut Element, Duration) {}
}
impl Element {
pub fn update(&mut self, tick: Duration) {
let keys = {
(&mut self.components).keys().clone()
};
for key in keys {
let mut component = self.components.remove(key).unwrap();
component.update(self, Duration::from_millis(0));
}
}
}
fn main() {}
错误
error[E0499]: cannot borrow `self.components` as mutable more than once at a time
--> src/main.rs:20:33
|
17 | (&mut self.components).keys().clone()
| --------------- first mutable borrow occurs here
...
20 | let mut component = self.components.remove(key).unwrap();
| ^^^^^^^^^^^^^^^ second mutable borrow occurs here
...
23 | }
| - first borrow ends here
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/main.rs:21:30
|
17 | (&mut self.components).keys().clone()
| --------------- first mutable borrow occurs here
...
21 | component.update(self, Duration::from_millis(0));
| ^^^^ second mutable borrow occurs here
22 | }
23 | }
| - first borrow ends here
keys()
method returns an iterator over the keys in the hashmap. The clone()
call only duplicates the iterator, but not the keys themselves. Your original approach with the map
function looks promising. You probably need to collect the result in a Vec
using the collect()
method of the iterator:
let keys = self.components.keys().cloned().collect::<Vec<_>>();
然后:
for key in keys {
self.components.remove(&key).unwrap();
}
这确保在删除操作开始之前复制所有密钥。
我有一个 Element
结构,它实现了一个更新方法,该方法需要一个滴答持续时间。该结构包含一个组件向量。允许这些组件在更新时修改元素。我在这里遇到借用错误,我不确定该怎么做。我尝试用块修复它,但块似乎不满足借用检查器的要求。
use std::collections::HashMap;
use std::time::Duration;
pub struct Element {
pub id: String,
components: HashMap<String, Box<Component>>,
}
pub trait Component {
fn name(&self) -> String;
fn update(&mut self, &mut Element, Duration) {}
}
impl Element {
pub fn update(&mut self, tick: Duration) {
let keys = {
(&mut self.components).keys().clone()
};
for key in keys {
let mut component = self.components.remove(key).unwrap();
component.update(self, Duration::from_millis(0));
}
}
}
fn main() {}
错误
error[E0499]: cannot borrow `self.components` as mutable more than once at a time
--> src/main.rs:20:33
|
17 | (&mut self.components).keys().clone()
| --------------- first mutable borrow occurs here
...
20 | let mut component = self.components.remove(key).unwrap();
| ^^^^^^^^^^^^^^^ second mutable borrow occurs here
...
23 | }
| - first borrow ends here
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/main.rs:21:30
|
17 | (&mut self.components).keys().clone()
| --------------- first mutable borrow occurs here
...
21 | component.update(self, Duration::from_millis(0));
| ^^^^ second mutable borrow occurs here
22 | }
23 | }
| - first borrow ends here
keys()
method returns an iterator over the keys in the hashmap. The clone()
call only duplicates the iterator, but not the keys themselves. Your original approach with the map
function looks promising. You probably need to collect the result in a Vec
using the collect()
method of the iterator:
let keys = self.components.keys().cloned().collect::<Vec<_>>();
然后:
for key in keys {
self.components.remove(&key).unwrap();
}
这确保在删除操作开始之前复制所有密钥。