Consider this example:
struct Item {
name: String,
}
struct Container {
name: String,
items: Vec<Item>,
}
impl Container {
fn do_something_with_item(&self, item: &mut Item) {
item.name.push_str(" is in container ");
item.name.push_str(&self.name);
}
}
fn main() {
let mut c = Container {
name: String::from("flugeldufel"),
items: vec![
Item {
name: String::from("hello"),
},
Item {
name: String::from("world"),
},
],
};
for mut item in &mut c.items {
c.do_something_with_item(&mut item);
println!("{}", item.name);
}
}
The error:
error[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
--> src/main.rs:30:9
|
29 | for mut item in &mut c.items {
| ------------
| |
| mutable borrow occurs here
| mutable borrow later used here
30 | c.do_something_with_item(&mut item);
| ^ immutable borrow occurs here
If I understand correctly, the method call borrows the whole of c
(because of &self
) even though it only uses name
from it and the iteration borrows c
as well (or at least c.items
).
What are my options?