How to get a list of files in a directory in Rust?
Hello everyone! I just started learning Rust and am trying to write a simple utility for working with files. I need to get a list of all files in a given directory. In Python I would use `os.listdir()`, but in Rust I can't find an equivalent.
I tried using `std::fs::read_dir()`, but I don't understand how to properly handle the result. Here's my code:
```rust
use std::fs;
fn main() {
let entries = fs::read_dir(".").unwrap();
for entry in entries {
println!("{:?}", entry);
}
}
```
It prints something like `Ok(DirEntry(...))`, but I need just the file names. Also I want to distinguish files from folders and possibly filter only files with a specific extension (e.g., `.txt`). I'm also interested in how to handle errors without `unwrap()`, because in a real project
No answers yet.
Log in to leave an answer