1

例如

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();
    }
}

在这种情况下,在我从中汲取经验的 C++ 中,me锁将被锁定,直到other.callfunc()发生之后。该变量不会超出范围并调用析构函数,直到范围定义的右大括号。

在 Rust 中,我知道借用检查器能够注意到它说“我不需要存在”的地方,它可以标记me为未借用并允许其他人借用它。这对优化器也适用吗?

编译器是否可能会调用隐式dropbefore other.callfunc(),还是语言设计不可能?

我找不到任何可以解决此问题的文档,因此我可以绝对确定。

4

0 回答 0