在C ++中(如果错误请纠正我),通过常量引用的临时绑定应该比它绑定的表达式更长。我认为在Rust中也是如此,但在两种不同情况下我得到了两种不同的行为。
考虑:
struct A;
impl Drop for A { fn drop(&mut self) { println!("Drop A.") } }
struct B(*const A);
impl Drop for B { fn drop(&mut self) { println!("Drop B.") } }
fn main() {
let _ = B(&A as *const A); // B is destroyed after this expression itself.
}
输出是:
Drop B.
Drop A.
这是你所期望的。但现在如果你这样做:
fn main() {
let _b = B(&A as *const A); // _b will be dropped when scope exits main()
}
输出是:
Drop A.
Drop B.
这不是我的预期。
这是有意的,如果是这样,那么两种情况下行为差异的理由是什么?
我正在使用Rust 1.12.1。
临时语句在语句结束时被删除,就像在C ++中一样。然而,IIRC,Rust中的破坏顺序是未指定的(我们将看到下面的结果),尽管当前的实现似乎只是按照构造的相反顺序丢弃值。
两者之间有很大的不同 let _ = x;
和 let _b = x;
。 _
不是Rust中的标识符:它是一个通配符模式。由于此模式未找到任何变量,因此最终值将在语句末尾有效删除。
另一方面, _b
是一个标识符,因此该值绑定到具有该名称的变量,该变量将其生命周期延长到函数结束。但是,那 A
实例仍然是临时的,因此它将在语句结束时删除(我相信C ++会做同样的事情)。由于语句结束在函数结束之前,所以 A
首先删除实例,然后删除 B
实例被删除第二。
为了更清楚,让我们在其中添加另一个声明 main
:
fn main() {
let _ = B(&A as *const A);
println!("End of main.");
}
这会产生以下输出:
Drop B.
Drop A.
End of main.
到现在为止还挺好。现在让我们来试试吧 let _b
;输出是:
Drop A.
End of main.
Drop B.
我们可以看到, Drop B
之后打印 End of main.
。这表明了 B
实例直到函数结束时才会生效,解释了不同的销毁顺序。
现在,让我们看看如果我们修改会发生什么 B
使用带有生命周期的借用指针而不是原始指针。实际上,让我们更进一步,删除 Drop
暂时实现:
struct A;
struct B<'a>(&'a A);
fn main() {
let _ = B(&A);
}
编译好了。在幕后,Rust为这两者分配了相同的生命周期 A
实例和 B
实例(即如果我们参考了 B
例如,它的类型将是 &'a B<'a>
哪两个 'a
是完全相同的寿命)。当两个值具有相同的生命周期时,我们必然需要将其中一个放在另一个之前,并且如上所述,订单未指定。如果我们加回来会发生什么 Drop
实现?
struct A;
impl Drop for A { fn drop(&mut self) { println!("Drop A.") } }
struct B<'a>(&'a A);
impl<'a> Drop for B<'a> { fn drop(&mut self) { println!("Drop B.") } }
fn main() {
let _ = B(&A);
}
现在我们得到一个编译器错误:
error: borrowed value does not live long enough
--> <anon>:8:16
|
8 | let _ = B(&A);
| ^ does not live long enough
|
note: reference must be valid for the destruction scope surrounding statement at 8:4...
--> <anon>:8:5
|
8 | let _ = B(&A);
| ^^^^^^^^^^^^^^
note: ...but borrowed value is only valid for the statement at 8:4
--> <anon>:8:5
|
8 | let _ = B(&A);
| ^^^^^^^^^^^^^^
help: consider using a `let` binding to increase its lifetime
--> <anon>:8:5
|
8 | let _ = B(&A);
| ^^^^^^^^^^^^^^
既然都是 A
实例和 B
实例已被赋予相同的生命周期,Rust无法推断这些对象的破坏顺序。错误来自Rust拒绝实例化的事实 B<'a>
与对象本身的生命周期 B<'a>
器物 Drop
(此规则是作为结果添加的 RFC 769 在Rust 1.0之前。如果允许, drop
将能够访问已被删除的值!但是,如果 B<'a>
没有实现 Drop
,那是允许的,因为我们知道没有代码会尝试访问 B
删除结构时的字段。
原始指针本身不带任何生命周期,因此编译器可能会执行以下操作:
例:
- B被创建(以便它可以容纳一个
*const A
在里面)
- A已创建
- B没有绑定绑定,因此被删除
- 不需要A因此被丢弃
我们来看看MIR:
fn main() -> () {
let mut _0: (); // return pointer
let mut _1: B;
let mut _2: *const A;
let mut _3: *const A;
let mut _4: &A;
let mut _5: &A;
let mut _6: A;
let mut _7: ();
bb0: {
StorageLive(_1); // scope 0 at <anon>:8:13: 8:30
StorageLive(_2); // scope 0 at <anon>:8:15: 8:29
StorageLive(_3); // scope 0 at <anon>:8:15: 8:17
StorageLive(_4); // scope 0 at <anon>:8:15: 8:17
StorageLive(_5); // scope 0 at <anon>:8:15: 8:17
StorageLive(_6); // scope 0 at <anon>:8:16: 8:17
_6 = A::A; // scope 0 at <anon>:8:16: 8:17
_5 = &_6; // scope 0 at <anon>:8:15: 8:17
_4 = &(*_5); // scope 0 at <anon>:8:15: 8:17
_3 = _4 as *const A (Misc); // scope 0 at <anon>:8:15: 8:17
_2 = _3; // scope 0 at <anon>:8:15: 8:29
_1 = B::B(_2,); // scope 0 at <anon>:8:13: 8:30
drop(_1) -> bb1; // scope 0 at <anon>:8:31: 8:31
}
bb1: {
StorageDead(_1); // scope 0 at <anon>:8:31: 8:31
StorageDead(_2); // scope 0 at <anon>:8:31: 8:31
StorageDead(_3); // scope 0 at <anon>:8:31: 8:31
StorageDead(_4); // scope 0 at <anon>:8:31: 8:31
StorageDead(_5); // scope 0 at <anon>:8:31: 8:31
drop(_6) -> bb2; // scope 0 at <anon>:8:31: 8:31
}
bb2: {
StorageDead(_6); // scope 0 at <anon>:8:31: 8:31
_0 = (); // scope 0 at <anon>:7:11: 9:2
return; // scope 0 at <anon>:9:2: 9:2
}
}
我们可以看到 drop(_1)
确实之前被称为 drop(_6)
假设,你得到上面的输出。
- 例
在此示例中,B绑定到绑定
- B被创建(出于与上述相同的原因)
- A已创建
- A没有约束而被删除
- B超出范围并被淘汰
相应的MIR:
fn main() -> () {
let mut _0: (); // return pointer
scope 1 {
let _1: B; // "b" in scope 1 at <anon>:8:9: 8:10
}
let mut _2: *const A;
let mut _3: *const A;
let mut _4: &A;
let mut _5: &A;
let mut _6: A;
let mut _7: ();
bb0: {
StorageLive(_1); // scope 0 at <anon>:8:9: 8:10
StorageLive(_2); // scope 0 at <anon>:8:15: 8:29
StorageLive(_3); // scope 0 at <anon>:8:15: 8:17
StorageLive(_4); // scope 0 at <anon>:8:15: 8:17
StorageLive(_5); // scope 0 at <anon>:8:15: 8:17
StorageLive(_6); // scope 0 at <anon>:8:16: 8:17
_6 = A::A; // scope 0 at <anon>:8:16: 8:17
_5 = &_6; // scope 0 at <anon>:8:15: 8:17
_4 = &(*_5); // scope 0 at <anon>:8:15: 8:17
_3 = _4 as *const A (Misc); // scope 0 at <anon>:8:15: 8:17
_2 = _3; // scope 0 at <anon>:8:15: 8:29
_1 = B::B(_2,); // scope 0 at <anon>:8:13: 8:30
StorageDead(_2); // scope 0 at <anon>:8:31: 8:31
StorageDead(_3); // scope 0 at <anon>:8:31: 8:31
StorageDead(_4); // scope 0 at <anon>:8:31: 8:31
StorageDead(_5); // scope 0 at <anon>:8:31: 8:31
drop(_6) -> [return: bb3, unwind: bb2]; // scope 0 at <anon>:8:31: 8:31
}
bb1: {
resume; // scope 0 at <anon>:7:1: 9:2
}
bb2: {
drop(_1) -> bb1; // scope 0 at <anon>:9:2: 9:2
}
bb3: {
StorageDead(_6); // scope 0 at <anon>:8:31: 8:31
_0 = (); // scope 1 at <anon>:7:11: 9:2
drop(_1) -> bb4; // scope 0 at <anon>:9:2: 9:2
}
bb4: {
StorageDead(_1); // scope 0 at <anon>:9:2: 9:2
return; // scope 0 at <anon>:9:2: 9:2
}
}
我们可以看到 drop(_6)
之前会被调用 drop(_1)
所以我们得到了你所看到的行为。