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.rs224 chars11 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