Path always returning false when trying to manually put a path with Rust

I've been learning Rust, and I'm having a bit of a problem with this code.

    let path = Path::new(&return_path); -- return path is the user input

    println!("{}", path.is_dir());

    match path.is_dir() {
        true => print!("yep its true"),
        false => print!("nope its false")

This always returns false. I'm trying to make a system where the user can provide a absolute path for example /home/user/Downloads and it checks if that is a valid directory to insert a file.

Double-check that the path exists. You'll get is_dir() == false also if the path doesn't exist. The path may be relative, and the executable running with a different current directory than you expect.

There's also a chance that your program doesn't have permission to access the path. If you're running on macOS, it will deny access to user directories like Desktop and Downloads until user approves the access, or allowlists the app in System Preferences.

1 Like

Also you could check that there's no unexpected line breaks on the end of input.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.