I'm trying to canonicalize a simple path containing the home directory ~ in Rust:
use std::path::PathBuf;
fn main() {
let path_str = "~"; // fails
// let path_str = ".."; // works
let path_buf = PathBuf::from(path_str);
let canon = path_buf.canonicalize().unwrap();
println!("{:?}", canon);
}
In my actual program, the ~ path comes from user input. This program fails on the .canonicalize().unwrap(), giving
Os { code: 2, kind: NotFound, message: "No such file or directory" }.
Is it a bug or a feature that Rust fails to canonicalize the ~?
What should I be doing instead?