Function dirs::home_dir

source ·
pub fn home_dir() -> Option<PathBuf>
Expand description

Returns the path to the user’s home directory.

The returned value depends on the operating system and is either a Some, containing a value from the following table, or a None.

PlatformValueExample
Linux$HOME/home/alice
macOS$HOME/Users/Alice
Windows{FOLDERID_Profile}C:\Users\Alice

§Linux and macOS:

  • Use $HOME if it is set and not empty.
  • If $HOME is not set or empty, then the function getpwuid_r is used to determine the home directory of the current user.
  • If getpwuid_r lacks an entry for the current user id or the home directory field is empty, then the function returns None.

§Windows:

This function retrieves the user profile folder using SHGetKnownFolderPath.

All the examples on this page mentioning $HOME use this behavior.

Note: This function’s behavior differs from std::env::home_dir, which works incorrectly on Linux, macOS and Windows.