If I have the following code:
struct HasDrop;
impl Drop for HasDrop {
fn drop(&mut self) {
println!("Dropping!");
}
}
fn main() {
println!("First");
{
let a = HasDrop;
if false {
let b = a;
}
println!("Middle")
}
println!("Last");
}
This prints:
First
Middle
Dropping!
Last
And if the condition in the if is true, it prints:
First
Dropping!
Middle
Last
This requires runtime overhead in that additional state must be stored with the object to allow the destruction of the object to happen at the end of the containing block, only if it was not moved as part of the true branch of the if statement.
What is the likelihood that this could be changed so that the drop always happens on the false branch of the if statement so that if the type is not moved, it is dropped instead. This would make things more consistent, in that objects would not be in a valid state while not being accessible (i.e. where the code above prints ‘Middle’).