This is a question from Rust quiz 28:
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
print!("1");
}
}
fn main() {
let _guard = Guard;
print!("3");
let _ = Guard;
print!("2");
}
Such code prints 3121, in the third line of main, assigning to _ means a immediate drop. However, when transferring the ownership to _ using the following code
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
print!("1");
}
}
fn main() {
let _guard = Guard;
print!("3");
let _ = _guard;
print!("2");
}
it prints 321, which means the Guard didn't drop immediately, and _ owns the Guard?
So I'm unsure when assigning a mutex to _ like this let _ = Mutex::lock().unwrap(), will it drop the mutex immediately?