How can I list files of a directory in Rust?

后端 未结 3 636
一整个雨季
一整个雨季 2021-02-04 23:21

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(\'./\')
3条回答
  •  花落未央
    2021-02-05 00:06

    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.

提交回复
热议问题