I have been learning rust lately and I am unable to figure out something
Why does this work
fn main() {
let mut s = String::from("hello");
println!("{}", &s);
let r = &s;
println!("{}", r);
let x = &mut s;
println!("{}", x);
}
but this doesn't
fn main() {
let mut s = String::from("hello");
println!("{}", &s);
let r = &s;
let x = &mut s;
println!("{}", r);
println!("{}", x);
}
It gives the following error:
cannot borrow `s` as mutable because it is also borrowed as immutable
If println takes the ownership of the string and doesn't return it, why does the first snippet work. Aren't we doing the same thing in both cases?