如何在 Rust 中列出目录的文件? [英] How can I list files of a directory in Rust?

查看:38
本文介绍了如何在 Rust 中列出目录的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Rust 中列出目录的所有文件?我正在寻找与以下 Python 代码等效的代码.

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('./')

推荐答案

使用 std::fs::read_dir().这是示例:

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.

这篇关于如何在 Rust 中列出目录的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆