I would like to iterate and filter entries with walkdir
, handling errors that may arise.
The following code does not compile, I understand why (?
is in a closure that returns &walkdir::DirEntry
), but I hope it clarifies what I am trying to achieve...
use anyhow::Result;
use walkdir::WalkDir;
fn main() -> Result<()> {
for entry in WalkDir::new(".")
.into_iter()
.map(|e| e?)
.filter(|e| e.file_type().is_file())
{
println("{}", entry.path().display());
}
Ok(())
}
The following code works, but silently ignores any errors that may arise:
use anyhow::Result;
use walkdir::WalkDir;
fn main() -> Result<()> {
for entry in WalkDir::new(".")
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
{
println("{}", entry.path().display());
}
Ok(())
}