1

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
stu
  • 8,461
  • 18
  • 74
  • 112
  • 3
    tl;dr the duplicate: no. Borrow lifetimes do not affect drop order, or codegen at all, really. – trent Jul 28 '20 at 01:07

0 Answers0