This code is driving me crazy now.
The function is defined:
fn change(x: & mut String) {
x.push_str(", changed");
}
The code with problem is:
let mut s6=String::from("s6~~~");
println!("before change: {s6}");
change(&mut s6);
println!("after change: {s6}");
let s7=&mut s6;
println!("{s7}");
change(s7);
println!("{s6}");//comment any line of these two will work
println!("{s7}");//comment any line of these two will work
I can only keep one of the last two lines, otherwise I got error message as:
error[E0502]: cannot borrow `s6` as immutable because it is also borrowed as mutable
--> src/main.rs:70:16
|
67 | let s7=&mut s6;
| ------- mutable borrow occurs here
...
70 | println!("{s6}");
| ^^ immutable borrow occurs here
71 | println!("{s7}");
| -- mutable borrow later used here
|
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
Why is printing treated as an ummutable borrow here in line 70?
I cannot move let s7=&mut s6;
line up to just under s6 definition either.
I tried to simplify the problem by commenting many lines:
let mut s6=String::from("s6~~~");
// println!("before change: {s6}");
// change(&mut s6);
// println!("after change: {s6}");
let s7=&mut s6;
println!("{s7}");
// change(s7);
println!("{s6}");//comment anyline of these two will work again
println!("{s7}");//comment anyline of these two will work again
Even this code doesn't compile, and the error message is the same. And if you comment any line of the last two, it will work again.
What is going on here, very Confusing! Thanks.