11//! Canonical definitions of `home_dir`, `cargo_home`, and `rustup_home`.
22//!
3- //! This provides the definition of `home_dir` used by Cargo and
4- //! rustup, as well functions to find the correct value of
5- //! `CARGO_HOME` and `RUSTUP_HOME`.
6- //!
7- //! See also the [`dirs`](https://docs.rs/dirs) crate.
8- //!
9- //! _Note that as of 2019/08/06 it appears that cargo uses this crate. And
10- //! rustup has used this crate since 2019/08/21._
11- //!
123//! The definition of `home_dir` provided by the standard library is
134//! incorrect because it considers the `HOME` environment variable on
145//! Windows. This causes surprising situations where a Rust program
178//! rustup use the standard libraries definition - they use the
189//! definition here.
1910//!
20- //! This crate further provides two functions, `cargo_home` and
11+ //! This crate provides two additional functions, `cargo_home` and
2112//! `rustup_home`, which are the canonical way to determine the
22- //! location that Cargo and rustup store their data.
13+ //! location that Cargo and rustup use to store their data.
14+ //! The `env` module contains utilities for mocking the process environment
15+ //! by Cargo and rustup.
2316//!
2417//! See also this [discussion].
2518//!
19+ //! See also the [`dirs`](https://docs.rs/dirs) crate.
20+ //!
2621//! [discussion]: https://github.com/rust-lang/rust/pull/46799#issuecomment-361156935
2722
2823#![ doc( html_root_url = "https://docs.rs/home/0.5.5" ) ]
@@ -36,29 +31,34 @@ mod windows;
3631use std:: io;
3732use std:: path:: { Path , PathBuf } ;
3833
39- /// Returns the path of the current user's home directory if known.
34+ /// Returns the path of the current user's home directory using environment
35+ /// variables or OS-specific APIs.
4036///
4137/// # Unix
4238///
4339/// Returns the value of the `HOME` environment variable if it is set
44- /// and not equal to the empty string. Otherwise, it tries to determine the
45- /// home directory by invoking the `getpwuid_r` function on the UID of the
46- /// current user.
40+ /// **even** if it is an empty string. Otherwise, it tries to determine the
41+ /// home directory by invoking the [`getpwuid_r`][getpwuid] function with
42+ /// the UID of the current user.
43+ ///
44+ /// [getpwuid]: https://linux.die.net/man/3/getpwuid_r
4745///
4846/// # Windows
4947///
50- /// Returns the value of the `USERPROFILE` environment variable if it
51- /// is set and not equal to the empty string. If both do not exist,
52- /// [`SHGetFolderPathW`][msdn] is used to return the appropriate path.
48+ /// Returns the value of the `USERPROFILE` environment variable if it is set
49+ /// **and** it is not an empty string. Otherwise, it tries to determine the
50+ /// home directory by invoking the [`SHGetFolderPathW`][shgfp] function with
51+ /// [`CSIDL_PROFILE`][csidl].
5352///
54- /// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpathw
53+ /// [shgfp]: https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpathw
54+ /// [csidl]: https://learn.microsoft.com/en-us/windows/win32/shell/csidl
5555///
5656/// # Examples
5757///
5858/// ```
5959/// match home::home_dir() {
60- /// Some(path) => println!("{}", path.display()),
61- /// None => println!("Impossible to get your home dir!"),
60+ /// Some(path) if !path.as_os_str().is_empty() => println!("{}", path.display()),
61+ /// _ => println!("Unable to get your home dir!"),
6262/// }
6363/// ```
6464pub fn home_dir ( ) -> Option < PathBuf > {
0 commit comments