struct C {
p: String,
q: String,
}
impl C {
fn call(&mut self) {}
}
fn main(){
let mut c = C { p: "p".to_string(), q: "q".to_string() };
let p = &mut c.p; // first mutable borrow occurs here
let q = &mut c.q; // second mutable borrow doesn't occur here, why???
c.call(); // second mutable borrow occurs here // error[E0499]: cannot borrow `c` as mutable more than once at a time
p.push('x');
q.push('y');
}
let q = &mut c.q; second mutable borrow doesn't occur here, why??? I really can't figure it out, can someone explain why in depth?
Because the compiler is smart enough to know that there isn't a mutable reference to
c.q. There is one toc.p, but not tocorc.q. That is called a split borrow: fields of a structure can be independently mutably borrowed.Note:
cis aMutexGuard, split borrowing doesn't work, whereas aBoxdoesFundamentally this is just a convenience, you could use pattern matching to hand-roll the split: