Pls explain why & how this program didn't compile

⚓ Rust    📅 2025-08-19    👤 surdeus    👁️ 4      

surdeus

#![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 = &mut x;

    f = &mut d;

    println!("{}", f);

    let mut zz = 4444;
    x = &mut zz;

    println!("{}", zz);
    println!("{}", z);
    println!("{}", f);
}

fn ok<'s>(arg1: &'s mut &mut u32, arg2: &'s mut u32) {}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `zz` as immutable because it is also borrowed as mutable
  --> src/main.rs:18:20
   |
16 |     x = &mut zz;
   |         ------- mutable borrow occurs here
17 |
18 |     println!("{}", zz);
   |                    ^^ immutable borrow occurs here
19 |     println!("{}", z);
20 |     println!("{}", f);
   |                    - 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 `z` as immutable because it is also borrowed as mutable
  --> src/main.rs:19:20
   |
4  |     let mut x = &mut z;
   |                 ------ mutable borrow occurs here
...
19 |     println!("{}", z);
   |                    ^ immutable borrow occurs here
20 |     println!("{}", f);
   |                    - 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

2 posts - 2 participants

Read full topic

🏷️ Rust_feed