2

I'm learning Rust and was referencing a book in which the author says that the following piece of code would give compile error:

fn main() {
    let mut x = 10;
    println!("x old val is {}", x);

    // {
        let y = &mut x;
        *y += 1;
    // }    

    println!("x new val is {}", x);
}

But to my surprise I'm not getting any error when I run it. Can somebody explain this example in detail. I'm using Rust version 1.41.0

Harry
  • 2,177
  • 1
  • 19
  • 33
  • 5
    This used to be disallowed, but later improvements in the compiler made this possible (as it's always safe to do). NLL are not really a new thing anymore, so your resource might be getting a bit old. – mcarton Feb 17 '20 at 15:18
  • 2
    These other people had the same question: [Why can the Rust compiler break borrowing rules when using Rust 1.31?](https://stackoverflow.com/q/53045978/3650362) and [Understanding of reference life time in Rust](https://stackoverflow.com/q/59206070/3650362) – trent Feb 17 '20 at 15:19

0 Answers0