'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.
Solution 1:[1]
Maybe double check &return_path? Here's a snippet to verify path works as expected:
use std::env;
use std::path::Path;
fn main() {
let arg = env::args().nth(1).unwrap(); // Get first argument
println!("Inspecting path {}", arg);
let path = Path::new(&arg);
println!("exists?: {}", path.exists());
println!("is_file?: {}", path.is_file());
println!("is_dir?: {}", path.is_dir());
println!("is_symlink?: {}", path.is_symlink());
if let Ok(full_path) = path.canonicalize() {
println!("Canonical path: {:?}", full_path);
} else {
println!("Couldn't canonicalize path");
}
}
And on my system:
> ./tmp /home/me/Downloads
Inspecting path /home/me/Downloads
exists?: true
is_file?: false
is_dir?: true
is_symlink?: false
Canonical path: "/home/me/Downloads"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marc_s |
