Rust does not allow mutable and immutable references at the same time. But the following code can be compiled and run:
struct MyStruct {
inner: i32,
}
fn main() {
let mut obj = MyStruct { inner: 0 };
println!("{:#?}", obj.inner);
let a = &obj;
println!("{}", a.inner);
let b = &mut obj;
println!("{}", b.inner);
// Compile error only when have this line:
// println!("{}", a.inner);
}
It is reasonable to invalidate references that will no longer be used later, which is understandable and convenient.
My question is, does this mean that useless variables or structs will be dropped in advance instead of until the end of the scope sometimes? Is "the structure or variable will definitely be droped at the end of the scope" always guaranteed by compiler?