Avatar ·

How to check if a file or directory exists in Rust? (Path::exists and alternatives)

Hello everyone! I'm a beginner in Rust and trying to write a simple program that checks if a file exists before reading it. I know that Python has a convenient `os.path.exists()`, but I haven't figured it out in Rust yet. Here's my code that doesn't work as I expect: ```rust use std::path::Path; fn main() { let path = Path::new("/tmp/test.txt"); if path.exists() { println!("File exists!"); } else { println!("File not found."); } } ``` The problem is that I'm not sure if I'm using `Path::exists()` correctly. I've also heard about `std::fs::metadata()`, but I don't understand the difference. I also need to check if the path is a directory rather than a file — how can I do that? Please advise which method is considered idiomatic in Rust. Should