Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list files of a directory in Rust?

Tags:

rust

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('./') 
like image 834
Graham Lee Avatar asked Sep 27 '14 15:09

Graham Lee


People also ask

How do I get a list of text contents of a folder?

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.

How can I get the list of files in a directory using C?

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.


1 Answers

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.

like image 108
Kai Sellgren Avatar answered Oct 16 '22 05:10

Kai Sellgren