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.
Walking a directory tree recursively in Rust scans a folder and all its subfolders to find every file inside. It's like opening a filing cabinet, checking every drawer, and then opening every folder inside those drawers to list everything you find. You use this when you need to process or list all files in a complex directory structure without writing your own loop logic.