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.

Read Files In A Directory Recursively In Rust

Code

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

use walkdir::{DirEntry, WalkDir};

fn is_hidden(entry: &DirEntry) -> bool {
    entry.file_name()
         .to_str()
         .map(|s| s.starts_with("."))
         .unwrap_or(false)
}

fn main() {
  let walker = WalkDir::new("recursive_test").into_iter();
  for entry in walker.filter_entry(|e| !is_hidden(e)) {
      let entry = entry.unwrap();
      println!("{}", entry.path().display());
  }
}

Results

recursive_test
recursive_test/example
recursive_test/example/a
recursive_test/example/a/2.txt
recursive_test/example/a/c
recursive_test/example/a/c/4.txt
recursive_test/example/a/c/delta.html
recursive_test/example/alfa.html
recursive_test/example/1.txt
recursive_test/example/bravo.html
recursive_test/example/b
recursive_test/example/b/3.txt
recursive_test/example/b/charlie.html