这是来自铁 rust 测验28的一道题:
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
print!("1");
}
}
fn main() {
let _guard = Guard;
print!("3");
let _ = Guard;
print!("2");
}
这样的代码打印3121
,在Main的第三行,赋值为_
表示立即丢弃.但是,当使用以下代码将所有权转移到_
时
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
print!("1");
}
}
fn main() {
let _guard = Guard;
print!("3");
let _ = _guard;
print!("2");
}
它打印321
,这意味着Guard
没有立即下降,而_
拥有Guard
?
所以我不确定当像let _ = Mutex::lock().unwrap()
这样将互斥锁赋给_
的时候,它会立即删除互斥锁吗?