How can I list all the files of a directory in Rust? I am looking for the equivalent of the following Python code.
files = os.listdir('./')
To do that, click Start, type CMD, then right-click Run as administrator. Change the directory to the folder you want to print the contents of. To do that, use the cd command—for example, “cd c:\users\yourusername\documents”. This will generate a basic text file listing the contents of the directory.
Call opendir() function to open all file in present directory. Initialize dr pointer as dr = opendir("."). If(dr) while ((en = readdir(dr)) != NULL) print all the file name using en->d_name.
Use std::fs::read_dir()
. Here's an example:
use std::fs; fn main() { let paths = fs::read_dir("./").unwrap(); for path in paths { println!("Name: {}", path.unwrap().path().display()) } }
It will simply iterate over the files and print out their names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With