From the home
crate.
The definition of
home_dir
provided by the standard library is incorrect because it relies on the$HOME
environment variable which has basically no meaning in Windows. This causes surprising situations where a Rust program will behave differently depending on whether it is run under a Unix emulation environment. Neither Cargo nor rustup use the standard libraries definition – instead they use the definition here.
There is discussion about bringing home_dir
back into the standard library, but for now the home
crate is probably your best option. It provides canonical definitions of home_dir
, cargo_home
, and rustup_home
:
match home::home_dir() {
Some(path) => println!("{}", path.display()),
None => println!("Impossible to get your home dir!"),
}