I'm unable to understand why below code errors out ?
fn main() {
let mut v = vec![];
let z = &mut v;
let y = &v;
z.push(1);
println!("{:?}",z); //accessing mutable borrow
}
In the above scenario, I'm not accessing y anywhere and rust should be able to compile it without any problems. I can see similar behavior happening in below code.
fn main() {
let mut v = vec![];
let y = &v;
let z = &mut v;
z.push(1);
println!("{:?}",z); // accessing mutable borrow
}
In the above scenario, rust doesn't complain on accessing mutable borrow because immutable borrow isn't accessed anywhere. For below scenario rust complains as the code is accessing immutable borrow, which is completely understandable.
fn main() {
let mut v = vec![];
let y = &v;
let z = &mut v;
z.push(1);
println!("{:?}",y); // accessing immutable borrow
}