How to get a list of files in a directory in Rust?
Hello everyone! I just started learning Rust and encountered a task that seems simple but is causing difficulties. I need to read the contents of a folder and get a list of all files in it. In Python, I would just use `os.listdir()`, but in Rust it's more complicated.\n\nI tried using `std::fs::read_dir`, but I don't fully understand how to properly process the result. Here's my code:\n\n```rust\nuse std::fs;\n\nfn main() {\n let paths = fs::read_dir(".").unwrap();\n for path in paths {\n println!("{:?}", path);\n }\n}\n```\n\nThis code outputs something like `Ok(DirEntry("./Cargo.toml"))` instead of just file names. How can I get a clean list of file names? Do I need to handle errors for each entry? And how do I filter only files, excluding folders?\n\nI would be grateful for any help.
No answers yet.
Log in to leave an answer