Open Scripter17 opened 1 year ago
EDIT: Dunno how I missed this, but there's partition_map
. So your example would just be:
fn get_dir(path: &path::Path) -> io::Result<(Vec<fs::DirEntry>, Vec<fs::DirEntry>)> {
Ok(path.read_dir()?
.filter_map(|content| content.ok())
.filter_map(|content| content.file_type().ok().map(|file_type|
if file_type.is_file() {
Either::Left(content)
} else {
Either::Right(content)
}
))
.partition_map(|x| x))
}
👍 to .partition_map(identity)
here.
Starting with the use case that prompted this:
Basically the same thing as the normal
partition
but the provided function has 3 return values:None
to drop the elementSome(true)
to put the element in the left side of the tupleSome(false)
to put the element in the right side of the tupleWithout
filter_partition
the best way I can think to get separate lists of files and folders is this:Which shouldn't ever cause issues unless a file's/folder's permissions are changed while the function is running, but that'd probably cause issues later on anyway.