For example
fn thing(&self, other: OtherThing) {
{
let me = self.lock.lock().unwrap();
...
me.do_something(); // last use of me
// me does not NEED to exist here
other.callfunc();
}
}
In this case, in C++, where I draw my experience from, the me
lock will be locked until after other.callfunc()
happens. The variable doesn't go out of scope and call the destructor until the closing brace of the scope definition.
In Rust, I know the borrow checker has the ability to notice that where it says "me does not NEED to exist" it can mark me
as not borrowed and allow somebody else to borrow it. Does that hold true for the optimizer also?
Is it possible that the compiler will call the implicit drop
before other.callfunc()
, or is that impossible by language design?
I couldn't find any documentation that clears this up so I can be absolutely sure.