rust - 为什么 rustc 的建议在这里是错误的?

标签 rust traits lifetime trait-objects

我正在尝试实现以下特征,其中 <'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/

相关文章:

rust - 当给出生命周期时,编译器在结构中询问生命周期

rust - Windows下的Rust编译产生 fatal error LNK1112

parsing - 使用 match 查看 stdin

scala - 算法混合

c++ - 还检查继承函数的函数签名

multithreading - 如何将对堆栈变量的引用传递给线程?

rust - 从闭包中返回时的RefMut生命周期错误

rust - 生命周期阻碍了状态机返回到最近的状态

scala - 如果特质以 self : Actor 开头,这意味着什么

rust - 无法在闭包内借用 Vec