Pls explain why error comes and how below code works

⚓ Rust    📅 2025-10-19    👤 surdeus    👁️ 2      

surdeus

#![allow(warnings)]
fn main() {
   

let mut z = 4;

// 'x = { z, 8, 16}
let mut x = & mut z;

let  mut s = 444; 

// 'd = { s, 13, 24}
let mut d = &mut s;

// 'f = { d, 16, 16 } // 'f1 = { z, 8, 16}
let mut f: &mut & mut u32 = &mut x;


// 'f = { d, 20, 24 } // 'f1 = { s, 13, 24}
f = &mut d;


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 `z` as immutable because it is also borrowed as mutable
  --> src/main.rs:23:15
   |
 8 | let mut x = & mut z;
   |             ------- mutable borrow occurs here
...
23 | println!("{}",z);
   |               ^ immutable borrow occurs here
24 | 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 1 previous error

4 posts - 4 participants

Read full topic

🏷️ Rust_feed