The most likely occurrence is that the book you're reading is teaching lifetimes disregarding non-lexical-lifetimes. This would make sense; lexical lifetimes are the most straightforward to understand.
Running the following will revert to prior to when non-lexical-lifetimes came around:
rustup default 1.30
This will revert rustc to prior to version 1.31
, which according to this document is the minimum version for nll.
Running this results in the exact same error as shown:
> cargo run
Compiling forum_examples v0.1.0 (C:\Users\user\Desktop\forum_examples)
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
--> src\main.rs:6:20
|
3 | let a_ref = &mut a;
| - mutable borrow occurs here
...
6 | println!("{}", a);
| ^ immutable borrow occurs here
7 | }
| - mutable borrow ends here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.
error: Could not compile `forum_examples`.
To learn more, run the command again with --verbose.
You may choose to use this version of the compiler (Or version 1.35 with 2015 edition) to follow the book to the letter or you may use this rule of thumb to determine why it doesn't compile according to the book but does with the compiler present today: The compiler will drop a reference if it should see that it is no longer needed. In your example, the compiler sees that a_ref
is no longer needed afterwards, so it's inserting an implicit drop right after for that. Note that this only works for references, and not for guards, or more complex types involving lifetimes (Especially not anything that could invoke drop
code).