9951 explained code solutions for 126 technologies


rustHow to iterate directory in Rust


Iterating through a directory in Rust can be done using the std::fs::read_dir function. This function takes a path as an argument and returns an iterator of std::fs::DirEntry objects.

Example code

use std::fs;

fn main() {
    let dir = "./";
    for entry in fs::read_dir(dir).unwrap() {
        let entry = entry.unwrap();
        println!("{:?}", entry.path());
    }
}

Output example

"./src"
"./Cargo.lock"
"./Cargo.toml"
"./target"

Code explanation

  • use std::fs;: imports the fs module from the std library.
  • let dir = "./";: sets the directory to iterate over.
  • fs::read_dir(dir).unwrap(): calls the read_dir function with the dir argument and unwraps the result.
  • let entry = entry.unwrap();: unwraps the entry object.
  • println!("{:?}", entry.path());: prints the path of the entry object.

Helpful links

Edit this code on GitHub