gpt4 book ai didi

recursion - 递归结构错误生命周期(无法为函数调用中的生命周期参数推断适当的生命周期... [E0495])

转载 作者:行者123 更新时间:2023-11-29 08:33:27 24 4
gpt4 key购买 nike

<分区>

我无法计算出此代码的生命周期参数。我尝试的所有操作通常都会导致编译器错误:

consider using an explicit lifetime parameter as shown

或者类似的东西

in type &'ent Entity<'a, 'ent>, reference has a longer lifetime than the data it references.

Entity , Reference是简化版本,以保持此示例最小化。

struct Entity<'a> {
id: i32,
name: &'a str,
references: Option<Vec<Reference<'a>>>,
}

struct Reference<'a> {
entity: &'a Entity<'a>,
}

fn main() {
let mut ents: Vec<Entity> = vec![Entity {
id: 0,
name: "Zero",
references: None,
},
Entity {
id: 1,
name: "One",
references: None,
},
Entity {
id: 2,
name: "Two",
references: None,
},
Entity {
id: 3,
name: "Three",
references: None,
}];
let references_ents_id = vec![vec![3, 1, 2], vec![1], vec![0, 3], vec![3, 0]];
create_references(&references_ents_id, &mut ents);
}

fn create_references(refs_id: &Vec<Vec<i32>>, ents_vec: &mut Vec<Entity>) {
for (id_ent, references) in refs_id.iter().enumerate() {
let mut references_of_ent: Vec<Reference> = vec![];
for id_ent in references {
references_of_ent.push(Reference {
entity: ents_vec.iter().find(|ent| ent.id == *id_ent).unwrap(),
});
}
ents_vec[id_ent].references = Some(references_of_ent);
}
}

Rust Playground

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com