考虑下面这段代码:
trait Trait {}
struct Child;
impl Trait for Child {}
struct Father<'a> {
child: &'a Box<dyn Trait>,
}
impl<'a> Trait for Father<'a> {}
fn main() {
let child: Box<dyn Trait> = Box::new(Child {});
let father: Box<dyn Trait> = Box::new(Father { child: &child });
let grandf: Box<dyn Trait> = Box::new(Father { child: &father });
}
该代码无法在Rust 1.30.0上编译,我收到了以下错误:
error[E0597]: `child` does not live long enough
--> src/main.rs:11:60
|
11 | let father: Box<dyn Trait> = Box::new(Father { child: &child });
| ^^^^^ borrowed value does not live long enough
12 | let grandf: Box<dyn Trait> = Box::new(Father { child: &father });
13 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
我可以通过使用
child: &'a Box<dyn Trait + 'a>
使代码编译,但我不明白为什么会这样。根据RFC 0599,默认对象约束规则应将类型&'a Box<Trait>
视为&'a Box<Trait + 'a>
。但实际上,它似乎是&'a Box<Trait + 'static>
。以下是需要回答的问题:
- 为什么我的原始代码无法编译?
- 默认对象绑定是否像看起来那样采用了
&'a Box<Trait + 'static>
?
&'a Box<SomeTrait>
类型和仅有Box<SomeTrait>
之间存在不同,使它们具有不同的默认生命周期。因此,在这种情况下,根据RFC,我认为盒装特征的默认生命周期应为'a
而不是'static
。这意味着要么有一个更新的RFC改变了RFC 0599的规定,要么还有另一个原因导致这段代码无法工作。在这两种情况下,那个问题的答案都不适用于这个问题,因此,这不是一个重复的问题。