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.

Do A Glob Search For Files In A Directory With Rust

Basic Recursive Glob

Code

//! ```cargo
//! [dependencies]
//! glob = "0.3.1"
//! ```


use glob::glob;

fn main() {
    for entry in glob("recursive_test/**/*.txt").expect("Failed to read glob pattern") {
        match entry {
            Ok(path) => println!("{:?}", path.display()),
            Err(e) => println!("{:?}", e),
        }
    }
}

Results

"recursive_test/example/.hidden_file_1.txt"
"recursive_test/example/.hidden_file_2.txt"
"recursive_test/example/1.txt"
"recursive_test/example/a/2.txt"
"recursive_test/example/a/c/4.txt"
"recursive_test/example/b/3.txt"

References

  • An alternative method for processing all the files and diretories directly instead of via the glob filtered set.