Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Find A File In A Directory Tree By Name In Rust

TODO: Fill this out

Code

use std::path::PathBuf;
use walkdir::WalkDir;

fn main() {
    let file_path = get_path_to_file("the-target");
    dbg!(&file_path);
}

fn get_path_to_file(target_name: &str) -> PathBuf {
    let asdf: Vec<_> = WalkDir::new(".")
        .into_iter()
        .filter_map(|v| {
            if let Some(name) = v.as_ref().unwrap().path().file_stem() {
                if name == target_name {
                    Some(v.as_ref().unwrap().path().display().to_string())
                } else {
                    None
                }
            } else {
                None
            }
        })
        .collect();
    PathBuf::from(&asdf[0])
}