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.

Get A List Of Files Recursively While Excluding Extensions And Hidden Files In Rust

Code

//! ```cargo
//! [package]
//! edition = "2021"
//! [dependencies]
//! walkdir = "2"
//! ```

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

fn main() {
    let files = get_file_list(
        "recursive_test/example",
        vec!["txt"]);
    dbg!(files);
}

fn get_file_list(dir: &str, ext: Vec<&str>) -> Vec<PathBuf> {
    WalkDir::new(dir)
    .into_iter()
    .filter(|e| 
      e.as_ref().ok().and_then(|x| x.file_name().to_str())
        .map(|s| !s.starts_with(".")
      ).unwrap_or(false)
    ).filter(|e| 
        match e
          .as_ref().unwrap().path().extension() {
            Some(x) => !ext.contains(&x.to_str().unwrap()),
            None => false
    }).map(|e| e.unwrap().into_path()).collect()
}

Results

[_active_nvim_run:15] files = [
    "recursive_test/example/a/c/delta.html",
    "recursive_test/example/alfa.html",
    "recursive_test/example/bravo.html",
    "recursive_test/example/b/charlie.html",
]