The map (and filter_map) maze

I'm trying to get only the file names (without the extensions) in a specific directory. This led me to something like this:

    .filter_map(|x| x.ok())
    .filter_map(|x| x.file_stem()
        .map(|y| y.to_os_string()))
    .for_each(|x| println!("{}", x.to_str().unwrap_or("-")));

Is there a better (or concise) way to do this? I'm new to rust but this look a bit on the ugly side to be honest (is this even the idiomatic way?)

Your code ignores errors with the first filter_map. I would probably write something along these lines:

for x in glob(&notes)? {
    let path = x?;
    if let Some(stem) = path.file_stem() {
        println!("{}", Path::new(stem).display());

Note that I used Path::display to display the stem rather than doing the to_os_string + to_str dance.


wow, your code definitely looks better, thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.