Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 1.5 KB

walk-directory-tree.md

File metadata and controls

50 lines (34 loc) · 1.5 KB
title timestamp author published description tags
Walk directory tree - traverse the filesystem
2024-04-19 08:40:01 -0700
szabgab
true
Walk the directory tree and print everything
walkdir

I needed this for the Rust Digger, but actually I bumped into the Walkdir crate totally by chance while I was looking for the crates with the biggest number of transitive reverse dependencies (popularity) on stats page of lib.rs. I saw memchr and looked at the looked at the GitHub Sponsors page of Andrew Gallant, the author of both.

Anyway, the crate has some very nice and clear examples and what you see here is basically the same, but I had to try it myself before I start using it in the application.

Dependency

{% include file="examples/walk-directory-tree/Cargo.toml" %}

Code

{% include file="examples/walk-directory-tree/src/main.rs" %}

The first example, that is actually commented out in the code will traverse the whole tree starting from the root we provide.

for entry in WalkDir::new(root) {
    let entry = entry.unwrap();
    println!("{}", entry.path().display());
}

The second example will skip some things, specifically it will skip the "target" folder.

This is the output I get in the folder of the crate I used for the example:

$ cargo run -q .
.
./Cargo.lock
./Cargo.toml
./src
./src/main.rs