Function homedir::unix::get_home

source ·
pub fn get_home<S: AsRef<str>>(
    username: S
) -> Result<Option<PathBuf>, GetHomeError>
Expand description

Get a user’s home directory path.

If some error occurs when obtaining the path, Err is returned. If no user associated with username could be found, Ok(None) is returned. Otherwise, the path to the user’s home directory is returned.

This function uses the User::from_name method provided by the nix crate. That method uses the getpwnam_r(3) library function to get the home directory from the /etc/passwd file.

Example

use homedir::get_home;

// This assumes there is a user named `root` which has
// `/root` as a home directory.
assert_eq!(
    std::path::Path::new("/root"),
    get_home("root").unwrap().unwrap().as_path()
);
assert!(get_home("nonexistentuser").unwrap().is_none());