我想处理io::Result<DirEntry>
返回从items
迭代返回std::fs::read_dir()
函数。我关心的是如何从Result
申请时match
时Ok
处理io ::结果<DirEntry>没有返回错误
let files = match fs::read_dir(&dir_path) {
Ok(items) => items,
//I actually want to leave function if there is an error here
Err(_) => return Err("Cannot read directory items".to_string()),
};
for item in files { // item: io::Result<DirEntry>
match item {
Ok(de) => de,// how to get `de` out of this scope??
//here I just want to print error and loop for next item
Err(_) => println!("{:?} cannot be accessed", item),
};
//do something with `de`
}
我想还有以下
let files = match fs::read_dir(&dir_path) {
Ok(items) => items,
Err(_) => return Err("Cannot read directory items".to_string()),
};
for item in files {
let file: DirEntry; // I get compile error for use of possibly uninitialized `file`
match item {
Ok(de) => file = de,
Err(_) => println!("{:?} cannot be accessed", item),
};
//do somthing with file
}
也许有处理Result
没有的情况下使用match
一种更好的方式获得的DirEntry
值喜欢这个?
看起来你并不需要把它弄出来的是范围,你可以做'好了的(de)=> {/ *做的东西去* /}' – wimh
我知道我能做到这但我想尽可能避免嵌套。 – MusuNaji