A simple example:
struct A; fn main() { test(2); test(1); } fn test(i: i32) { println!("test"); let a = A; if i == 2 { us(a); } println!("end"); } impl Drop for A { fn drop(&mut self) { println!("drop"); } }
When I run it, the output is:
test use drop end test end drop
I understand in the case of test(2) , a moves to us(a) , so it outputs "test-use-drop-end".
However, in test(1) output is "test-end-drop", which means that the compiler knows that a not been moved.
If us(a) is called, there is no need to drop a into test(i) , it will be reset to us(a) ; and if us(a) not called, a should be discarded after println!("end") .
Since it is impossible for the compiler to know whether us(a) is called or not, how does the compiler know if a.drop() will be called or not after println!("end") ?
rust
yyyy
source share