struct Foo; impl Drop for Foo { fn drop(&mut self) { println!("drop"); } } fn main() { let _ = Foo; println!("After"); }
http://is.gd/SQ6vcn
输出是
drop After
而如果我们换成:
struct Foo; impl Drop for Foo { fn drop(&mut self) { println!("drop"); } } fn main() { let x = Foo; let _ = x; println!("After"); }
http://is.gd/uvJMO0
After drop
虽然 eager drop 是很好的东西,但是,我们其实不需要啊!
我们明明可以显式地给返回值调用一个 drop!