Function homedir::unix::get_my_home
source · pub fn get_my_home() -> Result<Option<PathBuf>, GetHomeError>
Expand description
Get this process’ user’s home directory path.
This function will first check the $HOME
environment variable. If this variable
does not exist, then the /etc/passwd
file is checked.
The behaviour of this function is different from that of version 0.1.0.
Previously, this function would check the /etc/passwd
file first, and,
should that fail, it would only check the $HOME
environemnt variable if
the check_env
feature was set. Now, it will check the $HOME
environment
variable first, falling back on the /etc/passwd
file should that fail.
To replicate the original behaviour of the function, do get_home_from_id(get_my_id().unwrap())
.
Note that this can still return None
, should the /etc/passwd
file be missing an
entry for the user id of the program.
Example
use homedir::get_my_home;
// This assumes that the HOME environment variable is set to "/home/jpetersen".
assert_eq!(
std::path::Path::new("/home/jpetersen"),
get_my_home().unwrap().unwrap().as_path()
);