Possible Rust-specific optimizations

It allows the compiler to eliminate loads when an immutable place is known not to alias with any mutable ones:

fn print_and_increment(x: &i32, y: &mut i32) {
    println!("x = {}", x);
    *y += 1;
    println!("x = {}", x);
}

Since x and y are known not to alias, the compiler can assume *x doesn't change when something writes through y, so it can cache *x in a register instead of reloading it from main memory upon printing it for the second time.

7 Likes