本文簡要介紹rust語言中 Function std::env::home_dir
的用法。
用法
pub fn home_dir() -> Option<PathBuf>
如果已知,則返回當前用戶的主目錄的路徑。
Unix
- 如果設置了 'HOME' 環境變量,則返回它的值(包括空字符串)。
- 否則,它會嘗試通過使用當前用戶的 UID 調用
getpwuid_r
函數來確定主目錄。getpwuid_r
函數返回的空主目錄字段被視為有效值。 - 如果當前用戶在 /etc/passwd 文件中沒有條目,則返回
None
。
Windows
- 如果設置了 'HOME' 環境變量,則返回它的值(包括空字符串)。
- 否則,返回 'USERPROFILE' 環境變量的值(如果已設置)(包括空字符串)。
- 如果兩者都不存在,則使用
GetUserProfileDirectory
返回路徑。
例子
use std::env;
match env::home_dir() {
Some(path) => println!("Your home directory, probably: {}", path.display()),
None => println!("Impossible to get your home dir!"),
}
相關用法
- Rust hash用法及代碼示例
- Rust hard_link用法及代碼示例
- Rust UdpSocket.set_multicast_loop_v6用法及代碼示例
- Rust i64.overflowing_add_unsigned用法及代碼示例
- Rust Box.downcast用法及代碼示例
- Rust BTreeMap.last_key_value用法及代碼示例
- Rust str.make_ascii_uppercase用法及代碼示例
- Rust u128.checked_pow用法及代碼示例
- Rust usize.wrapping_mul用法及代碼示例
- Rust AtomicU8.fetch_sub用法及代碼示例
- Rust PanicInfo.payload用法及代碼示例
- Rust MaybeUninit.assume_init_mut用法及代碼示例
- Rust String.try_reserve用法及代碼示例
- Rust Mutex.new用法及代碼示例
- Rust f32.exp用法及代碼示例
- Rust Result.unwrap_or_else用法及代碼示例
- Rust slice.sort_unstable_by_key用法及代碼示例
- Rust Formatter.precision用法及代碼示例
- Rust i128.log2用法及代碼示例
- Rust OsStr.to_ascii_uppercase用法及代碼示例
- Rust f32.hypot用法及代碼示例
- Rust RefCell.try_borrow_unguarded用法及代碼示例
- Rust i16.log10用法及代碼示例
- Rust LowerExp用法及代碼示例
- Rust HashSet.get_or_insert_with用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Function std::env::home_dir。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。