#![allow(warnings)]
fn main() {
let mut z = 4;
let mut x = & mut z;
//-----------------------------
let mut s = 444;
let mut d = &mut s;
//------------------------------
let mut f: &mut & mut u32;
f = &mut x;
f = &mut d;
println!("{}",f);
//-------------------------------
let mut zz = 4444;
let mut ss = 4444;
d= & mut zz;
x = & mut ss;
println!("{}",zz);
println!("{}",ss);
println!("{}",d);
}
Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `zz` as immutable because it is also borrowed as mutable
--> src/main.rs:35:15
|
31 | d= & mut zz;
| -------- mutable borrow occurs here
...
35 | println!("{}",zz);
| ^^ immutable borrow occurs here
...
38 | println!("{}",d);
| - mutable borrow later used here
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0502]: cannot borrow `ss` as immutable because it is also borrowed as mutable
--> src/main.rs:36:15
|
32 | x = & mut ss;
| -------- mutable borrow occurs here
...
36 | println!("{}",ss);
| ^^ immutable borrow occurs here
37 |
38 | println!("{}",d);
| - mutable borrow later used here
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0502`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
True. And none of the errors you posted suggest as much. They clearly describe what the issue is and aside from the varying variable names, the issue is the same as in your last question(s).
Edit:On second sight, you might have found a compiler bug.
In the second error message, the mutable borrow d really has nothing to do with x = &mut ss;.
The problem appears to stem from the same lifetime issues as described by @quinedot in the linked post.
The explanation for this one is much simpler. Here's a reduction of the code.
fn main() {
let mut zz = 4444;
let d = &mut zz;
println!("{}",zz);
println!("{}",d);
}
&mut _ are exclusive references. An exclusive reference being active conflicts with the referent being used directly. In the code, the exclusive borrow of zz is active from the &mut zz through the println!("{}", d), so the use of zz in the println!("{}",zz) conflicts with it.
The above error is by design. Violating the exclusiveness of &mut _ is considered undefined behavior. Understanding why the simple snippet above is an error is a fundamental part of understanding &mut _.
The errors for other code you've posted, the ones which are accepted by Polonius, are probably just shortcomings of the current borrow checker. I wouldn't classify such as a bug either, unless it's a regression. Understanding why various permutations of reassigning nested references works or doesn't work in the current borrow checker isn't very important, especially if the next borrow checker is going to accept it (i.e. especially if it's sound to accept it). Code like that doesn't really come up in practice.
People are assuming you're posting the same question over and over again because your past 5 posts -- that's every post of yours in more than a year -- have been similar contrived variations around reassigning nested references. I doubt you'll get very good interaction so long as that continues.
I believe that I understand your problem, but you really don't make much effort of conveying your actual issue.
For the above example you get the error:
Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `ss` as immutable because it is also borrowed as mutable
--> src/main.rs:36:15
|
32 | x = & mut ss;
| -------- mutable borrow occurs here
...
36 | println!("{}",ss);
| ^^ immutable borrow occurs here
37 |
38 | println!("{}",d);
| - mutable borrow later used here
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0502`.
error: could not compile `playground` (bin "playground") due to 1 previous error
And, indeed, 38 | println!("{}",d); appears unrelated to x = & mut ss;
I believe it stems from
f = &mut x;
f = &mut d;
Because, if you comment out either of those lines, the code compiles.
I never came across such a contrived example, which is why I initially thought of a possible compiler error regarding this line.
But I'm not an expert on contrived nested borrow errors. As @quinedot mentioned, those don't come up in my everyday work.
Maybe somebody with more knowledge on the finer details of the compiler and borrow checker may help you out here.
At any rate, you'd probably get better interaction, if you'd actually put in some legwork and guide the people here towards your actual issue, what you'd expect and what the desired outcome would be.