In reading Hands-On Concurrency with Rust, I came across an example which is similar to an example that was shown in the old Rust book:
fn main() {
let mut x = 5;
let y = &mut x;
*y += 1;
println!("{}", x);
}
In both books, they explain that this would not compile. Both books say that the println!
macro is implicitly borrowing from x
. The old Rust book said the following:
We have a &mut T pointing to x, and so we aren’t allowed to create any &Ts
"Hands-On Concurrency" added this:
Were this program to compile, we would be subject to a race between the update of y and the read of x, depending on the CPU and memory ordering.
Both of these explanations seem to make sense, however if I compile and run this code on the latest versions of Rust stable (both 1.36 and 1.37) the code compiles and runs fine.
What has changed in Rust that allows this to compile while still avoiding the potential issues that caused older versions of Rust to produce compiler errors?