How to Walk a Directory Tree Recursively in Rust (walkdir)

Use the walkdir crate with WalkDir::new to recursively iterate over all files and directories in a tree.

Use the walkdir crate's DirEntry iterator to traverse directories recursively. Add walkdir = "2.3.1" to your Cargo.toml dependencies, then import and call walkdir in your code:

use walkdir::WalkDir;

fn main() {
    for entry in WalkDir::new("/path/to/dir").into_iter().filter_map(|e| e.ok()) {
        println!("{:?}", entry.path());
    }
}

This iterates through every file and directory, printing the path of each entry.