list all avaiable files in a directory in rust

To list all available files in a directory in Rust, we can use the std::fs module provided by Rust's standard library.

Here's an example code that lists all files in a specified directory:

main.rs
use std::fs;

fn main() {
    let paths = fs::read_dir("/path/to/directory").unwrap();

    for path in paths {
        let file_name = path.unwrap().file_name();
        println!("{}", file_name.to_string_lossy());
    }
}
224 chars
11 lines

The fs::read_dir() function returns an iterator over the contents of the directory specified by the input parameter. We use unwrap() to handle any possible errors that may occur.

Then, for each file in the directory, we extract the file name using the file_name() method and print it out using println!(). The to_string_lossy() method is used to convert the file name to a string.

Note: If the directory path is a relative path, it should be relative to the current working directory of the program.

gistlibby LogSnag