简体   繁体   English

“结构中的生命周期:”此值要求 `'1` 必须比 `'static` 寿命长

[英]"Lifetime in struct : "this value requires that `'1` must outlive `'static`

I am new to the concept of lifetime but I don't understand why the current code gives me an error我对生命周期的概念不熟悉,但我不明白为什么当前代码会给我一个错误

error -> [1]: https://i.stack.imgur.com/v4Yf6.png错误-> [1]: https://i.stack.imgur.com/v4Yf6.png

Code:代码:

pub struct Folder<'a> {
    pub name: &'a str,
    pub contents: Vec<Box<dyn Executable>>, 
    pub parent: Box<Option<&'a Folder<'a>>>
}

impl<'a> Folder<'a> {
    pub fn new(folder_name: &'a str, json_content: &'a Value) -> Self {
        Self {
            name: folder_name,
            contents: Folder::get_content(json_content),
            parent: Box::new(None)
        }
    }

    fn get_content(json_content: &Value) -> Vec<Box<dyn Executable>> {
        let elements_in_folder: &Vec<Value> = json_content.as_array().unwrap();
        let mut contents: Vec<Box<dyn Executable>> = Vec::new();

        for element in elements_in_folder.iter() {
            match element["type"].as_str().unwrap() {
                "folder" => {
                    let folder = Folder::new(
                        element["name"].as_str().unwrap(),
                        &element["content"],
                    );
                    contents.push(Box::new(folder))
                }

                "command" => contents.push(Box::new(Command::new(&element))),

                "combo" => contents.push(Box::new(Combo::new(&element))),

                _ => panic!("Folder: field type unknow"),
            }
        }
        contents  // THE ERROR IS HERE 
    }
}

Folder Command and Combo impl Executable文件夹命令和组合 impl 可执行文件

Your contents is borrowing values from inside the argument.您的contents是从参数内部借用值。 You need to make that explicit, with a lifetime constraint.您需要明确说明这一点,并具有生命周期约束。

fn get_content<'a>(json_content: &'a Value) -> Vec<Box<dyn Executable + 'a>> {
  ...
}

or, equivalently, you can use the anonymous lifetime.或者,等效地,您可以使用匿名生命周期。 The following is equivalent to the above but shorter.以下等同于上述但更短。

fn get_content(json_content: &Value) -> Vec<Box<dyn Executable + '_>> {
  ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rust 生命周期参数必须比静态生命周期更长 - rust lifetime parameter must outlive the static lifetime 异步关闭中的错误:生命周期可能不够长; 返回此值要求 `'1` 必须比 `'2` 寿命更长 - Error in Async closure: Lifetime may not live long enough; returning this value requires that `'1` must outlive `'2` Rust:“返回此值要求 `'op` 必须比 `'static` 存活得更久” - Rust: "returning this value requires that `'op` must outlive `'static`" with HRTB in closure returning future 结构必须超过成员引用 - Struct must outlive member reference &lt;&#39;a,&#39;b:&#39;a&gt;是否意味着生命周期&#39;b必须比生命周期更长&#39;a? - Does <'a, 'b: 'a> mean that the lifetime 'b must outlive the lifetime 'a? 对 Actix 服务器中捕获的变量的引用会导致“参数要求它必须比 &#39;静态”存活时间更长 - References to captured variables in an Actix server causes "argument requires that it must outlive 'static" 请求消息值必须在静态生命周期内有效 - Request message value must be valid for the static lifetime Rust:未来的工厂方法导致编译器错误:值要求“1”必须比“2”更长寿 - Rust: future factory method results in compiler error: value requires that `'1` must outlive `'2` `format!` 需要静态生命周期吗? - `format!` requires static lifetime? 生命周期必须在 static 生命周期内有效 - the lifetime must be valid for the static lifetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM