【发布时间】:2024-04-20 04:35:02
【问题描述】:
嗨,我在这里找到了一些代码,似乎可以从函数返回 Vec,但我无法理解代码。代码如下,我添加了 cmets 以显示我在哪里感到困惑。
fn read_filename_from_dir<P>(path: P) -> Result<Vec<PathBuf>, io::Error> where P: AsRef<Path>,{
fs::read_dir(path)? //returns result<readDir> ? unwraps to readDir
.into_iter() //creates a new iter containing result<DirEntry>
.map(|x| x.map(|entry| entry.path()))
.collect() //the double map confuses me. this is where i lose understanding
}
我假设既然我们有一个包含 Result 的迭代器,我们是否必须首先解开每个 DirEntry 然后只映射一次解包,或者双映射是否以某种方式解包? 你将如何解开结果的迭代?使用 map(|x| x.unwrap()) ?
【问题讨论】:
标签: rust file-io iterator unwrap