This compiles:
struct Container {
list: Vec<String>
}
fn main() {
let mut c = Container{ list: vec![String::from("asdf")] };
let borrow = &c.list[0]; // Borrow starts here
let copy = borrow.clone(); // Last use of borrow
foo(&mut c); // Can borrow again
println!("{}", copy);
}
fn foo(_container: &mut Container) { }
This does not:
struct Container {
list: Vec<String>
}
fn main() {
let mut c = Container{ list: vec![String::from("asdf")] };
for borrow in &c.list { // Borrow starts here
let copy = borrow.clone(); // Last use of borrow
foo(&mut c); // Cannot borrow again???
println!("{}", copy);
}
}
fn foo(_container: &mut Container) { }
error[E0502]: cannot borrow `c` as mutable because it is also borrowed as immutable
--> src/main.rs:11:13
|
8 | for borrow in &c.list { // Borrow starts here
| -------
| |
| immutable borrow occurs here
| immutable borrow later used here
...
11 | foo(&mut c); // Cannot borrow again???
| ^^^^^^ mutable borrow occurs here
In my mind this does the exactly same thing, doesn't it?
Do I have to manually "end" the borrow? Can I? How?