借用的值的生命周期不够长,需要静态生命周期

San*_* Li 3 lifetime rust

我在这个Rust Playground 的示例代码中收到此错误

   Compiling playground v0.0.1 (/playground)
error[E0597]: `text` does not live long enough
  --> src/main.rs:34:38
   |
34 |     let item = NotLongEnough { text: &text };
   |                                      ^^^^^ borrowed value does not live long enough
35 |     let mut wrapper = Container { buf: Vec::new() };
36 |     wrapper.add(Box::new(item));
   |                 -------------- cast requires that `text` is borrowed for `'static`
...
40 | }
   | - `text` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
error: could not compile `playground`

To learn more, run the command again with --verbose.
Run Code Online (Sandbox Code Playgroud)

内容是:

trait TestTrait {
    fn get_text(&self) -> &str;
}

#[derive(Copy, Clone)]
struct NotLongEnough<'a> {
    text: &'a str,
}

impl<'a> TestTrait for NotLongEnough<'a> {
    fn get_text(&self) -> &str {
        self.text
    }
}

struct Container {
    buf: Vec<Box<dyn TestTrait>>,
}

impl Container {
    pub fn add(&mut self, item: Box<dyn TestTrait>) {
        self.buf.push(item);
    }

    pub fn output(&self) {
        for item in &self.buf {
            println!("{}", item.get_text());
        }
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let text = "test".to_owned();
    let item = NotLongEnough { text: &text };
    let mut wrapper = Container { buf: Vec::new() };
    wrapper.add(Box::new(item));
    wrapper.output();

    Ok(())
}

Run Code Online (Sandbox Code Playgroud)

我不知道为什么cast requires that text is borrowed for 'static 有人可以帮助我吗?我不知道我做错了什么。

Sve*_*rev 5

TLDR:修复版本

问题出在你的Container定义中:

struct Container {
    buf: Vec<Box<dyn TestTrait>>,
}
Run Code Online (Sandbox Code Playgroud)

该语句dyn TestTrait相当于dyn TestTrait + 'static,这意味着您的特征对象不得包含任何生命周期小于 的引用'static

为了解决这个问题,你必须用一个不太严格的特征边界来替换这个特征边界:

struct Container<'a> {
    buf: Vec<Box<dyn TestTrait + 'a>>,
}
Run Code Online (Sandbox Code Playgroud)

现在'static,容器需要'a. 您还必须将该更改应用到实现中:

   pub fn add(&mut self, item: Box<dyn TestTrait + 'a>) { // notice the new trait-bound
        self.buf.push(item);
   }
Run Code Online (Sandbox Code Playgroud)

相关资源: