gpt4 book ai didi

rust - Rust:创建结构,借入/生命周期问题的向量时为E0597

转载 作者:行者123 更新时间:2023-12-03 11:46:31 25 4
gpt4 key购买 nike

我对Rust还是很陌生,并且一直在与所有权机制进行斗争。
我想遍历本地目录并将所有单个元素收集到一个结构中。然后将这些结构放入向量中以备后用。因此,基本上我正在尝试构建结构的向量。
结构的前两个字段应分别包含路径各个部分的向量,后两个字段应为OsString:

struct FileItem<'a> {
g_base_dir: Vec<Component<'a>>, //base directory
o_full_uri: Vec<Component<'a>>, //original full path
o_filename: OsString, //original filename
o_extension: OsString, //original extension
}
我使用“WalkDir”遍历目录并为每个单独的项目获取“DirEntry”。我的问题是我无法将构造的结构推到向量上:
fn main() {
let mut file_item_collection: Vec<FileItem> = Vec::new();

let base_dir = Path::new("D:\\test");

for entry in WalkDir::new(base_dir).into_iter().filter_map(|e| e.ok()) {
let current_file = FileItem {
g_base_dir: base_dir.components().collect::<Vec<_>>(),
o_full_uri: entry.path().components().collect::<Vec<_>>(),
o_filename: integrity_check(1, &entry).unwrap(),
o_extension: integrity_check(2, &entry).unwrap(),
};

file_item_collection.push(current_file);
}

for item in &file_item_collection {
println!("{:?}", item.o_full_uri);
}
o_full_uri的分配失败,因为:
`entry` does not live long enough
borrowed value does not live long enough rustc(E0597)
main.rs(56, 5): `entry` dropped here while still borrowed
main.rs(55, 9): borrow later used here
我知道 entry不在范围内,但是我不知道该怎么办。
任何帮助将不胜感激!

考虑到@Hadus和@kmdreko的输入,解决方案看起来像这样,并且运行良好:
struct FileItem {
g_base_dir: Vec<OsString>, //global base directory
o_full_uri: Vec<OsString>, //original full path
o_filename: OsString, //original filename
o_extension: OsString, //original extension
}
fn main() {
let mut file_item_collection: Vec<FileItem> = Vec::new();

let base_dir = Path::new("D:/test");

let entries: Vec<DirEntry> = WalkDir::new(base_dir)
.into_iter()
.filter_map(|e| e.ok())
.collect();

for entry in &entries {
if entry.path().is_file() {
let current_file = FileItem {
g_base_dir: base_dir
.components()
.map(|e| e.as_os_str().to_os_string())
.collect(),
o_full_uri: entry
.path()
.components()
.map(|e| e.as_os_str().to_os_string())
.collect(),
o_filename: entry.path().file_stem().unwrap_or_default().to_os_string(),
o_extension: entry.path().extension().unwrap_or_default().to_os_string(),
};
file_item_collection.push(current_file);
} else {
continue;
}
}

谢谢!

最佳答案

您对entry的生命周期不够长的问题是正确的。考虑一下这种简化:

let mut things = vec![];

{
// things inside here live till the end of this scope
let entry = 7;

// this won't work because entry will be dropped when
// the scope exits and we would be pointing to nothing
things.push(&entry);
}

// here entry doesn't exist anymore
问题是您试图将指向内部作用域的引用存储在外部作用域中。您必须仅具有指向相同范围或更高范围的引用。
例如,这很好(范围相同):
let mut things = vec![];

// lives as long as things
let entry = 7;

{
// this will work
things.push(&entry);
}

// here entry and things still both exist
这也很好(更高的范围):
// lives longer than things
let entry = 7;

{
let mut things = vec![];

// this will work
things.push(&entry);
}
我们必须确保在 entry循环的末尾(使用相同的作用域)不会丢弃 while:
fn main() {
let mut file_item_collection: Vec<FileItem> = Vec::new();

let base_dir = Path::new("D:\\test");

// the entries are now stored outside the while loop
// so they do live long enough
let entries: Vec<DirEntry> = WalkDir::new(base_dir)
.into_iter()
.filter_map(|e| e.ok())
.collect();

for entry in &entries {
...
}

...
}

关于rust - Rust:创建结构,借入/生命周期问题的向量时为E0597,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66359123/

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