I'm learning Rust and looking for better/cleaner ways to list all file names that do not start with filter
, given a pathname
as a parameter.
fn list_files(&self, pathname: &PathBuf, filter: &str) -> Result<Vec<PathBuf>, Error> {
fs::read_dir(pathname).map(|read_dir| {
read_dir
.filter_map(|res| {
res.map(|entry| {
entry
.path()
.strip_prefix(pathname)
.ok()
.map(|path| {
if path.starts_with(filter) {
None
} else {
Some(path.to_path_buf())
}
})
.flatten()
})
.ok()
.flatten()
})
.collect()
})
}