我正在尝试实现以下特征,其中 <'baz>
以后需要 impl<'baz> Baz<'baz> for T where T: OtherTrait
†.
trait Baz<'baz>: Clone {
fn baz(&'baz mut self);
fn bazzed(&self) -> Self {
let mut x = self.clone();
x.baz();
x
}
}
Rustc 失败是可以理解的:
Compiling playground v0.0.1 (/playground)
error[E0309]: the parameter type `Self` may not live long enough
--> src/lib.rs:6:9
|
6 | x.baz();
| ^
|
= help: consider adding an explicit lifetime bound `Self: 'baz`...
= note: ...so that the type `Self` is not borrowed for too long
error[E0309]: the parameter type `Self` may not live long enough
--> src/lib.rs:6:11
|
6 | x.baz();
| ^^^
|
= help: consider adding an explicit lifetime bound `Self: 'baz`...
= note: ...so that the reference type `&'baz mut Self` does not outlive the data it points at
For more information about this error, try `rustc --explain E0309`.
error: could not compile `playground` due to 2 previous errors
所以我听从了它的建议:
trait Baz<'baz>: Clone {
fn baz(&'baz mut self);
fn bazzed(&self) -> Self
where
Self: 'baz
{
let mut x = self.clone();
x.baz();
x
}
}
而且我不太明白它现在不喜欢什么...
Compiling playground v0.0.1 (/playground)
error[E0597]: `x` does not live long enough
--> src/lib.rs:9:9
|
1 | trait Baz<'baz>: Clone {
| ---- lifetime `'baz` defined here
...
9 | x.baz();
| ^^^^^^^
| |
| borrowed value does not live long enough
| argument requires that `x` is borrowed for `'baz`
10 | x
11 | }
| - `x` dropped here while still borrowed
error[E0505]: cannot move out of `x` because it is borrowed
--> src/lib.rs:10:9
|
1 | trait Baz<'baz>: Clone {
| ---- lifetime `'baz` defined here
...
9 | x.baz();
| -------
| |
| borrow of `x` occurs here
| argument requires that `x` is borrowed for `'baz`
10 | x
| ^ move out of `x` occurs here
Some errors have detailed explanations: E0505, E0597.
For more information about an error, try `rustc --explain E0505`.
error: could not compile `playground` due to 2 previous errors
我试着制作 Self: 'static
也是,但这也无济于事。我在这里缺少什么?
† <'baz>
我需要另一个特征,大致如下:
trait ReturnIteratorMut<'item> {
type IterType: Iterator<Item = &'item mut Thing>;
fn iterator<'slf: 'item>(&'slf mut self) -> Self::IterType;
}
我想实现 Baz
所有这些通过应用 baz()
到迭代器中的每个项目,所以我需要 'baz: 'item
.这适用于 baz()
方法,它只是 bazzed()
它开始提示。
最佳答案
在没有编译器帮助的情况下,让我们首先尝试理解这段代码有什么问题。
x.baz()
需要 x
为 'baz
生存。由于 'baz
可以是任何生命周期,这意味着 x
必须为 'static
存在。它不是 - 它是一个局部变量,这是一个错误(你可能想要 HRTB,但你说你想关注编译器错误,所以我不处理解决方案)。
编译器的第二个错误就是 - x
的生命周期不够长,或者,如果我们从相反的角度看,x
是为 借用的' static
,所以我们不能移动它(编译器显示这两个错误以最大化显示的错误 - 有时它有效,有时,就像在这种情况下,它会导致显示令人困惑的错误)。
第一个错误是这个约束的一个微不足道的结果,但它隐藏了实际的错误(就其值(value)而言,on nightly they're all shown together):x
needs to live for ' static
,但不仅如此,有时它的类型的任何变量都不可能是。假设 Self
是 &'some_short_lifetime i32
- 它不能为 'static
生存,因为它包含更短的生命周期。这就是“Self
可能活得不够长”的原因。
关于rust - 为什么 rustc 的建议在这里是错误的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72981907/