std::env
이 페이지의 내용
API 참조는 영어로만 있습니다. 표준 라이브러리 소스의 영어 문서 주석에서 만듭니다.
arguments, environment variables, directories.
var, vars, vars_os, set_var and remove_var share one lock;
nothing guards a getenv in C code, so set_var / remove_var stay
unsafe.
Names and values from the OS are bytes (Windows: its UTF-16 as WTF-8);
var hands out the UTF-8 ones as str, args / vars replace what
is not UTF-8 with U+FFFD, and the *_os forms keep the bytes.
Structs
RawStrSource
pub struct RawStrBytes from the OS that need not be UTF-8 (args_os, var_os).
Byte length.
truewhen there are no bytes.The bytes as text; throws if they are not UTF-8.
The text with each ill-formed part as U+FFFD (Windows: each lone surrogate).
The bytes, as a read-only view (no copy).
UTF-16 units from the OS, lone surrogates allowed.
The UTF-16 units, as a view of a list made for it.
Trait implementations 1
impl Debug for RawStr
VarsSource
pub struct VarsThe iterator vars returns.
VarsOsSource
pub struct VarsOsThe iterator vars_os returns.
JoinPathsErrorSource
pub struct JoinPathsErrorA join_paths entry that holds the separator (or, on Windows, a
").
The path that cannot be joined.
What went wrong, for display.
Enums
VarErrorSource
pub enum VarErrorWhy var gave no str.
The variable does not exist.
Its value is not UTF-8 (read it with
var_os).What went wrong, for display.
Functions
argsSource
pub fn args() -> List<str> Command-line arguments; [0] is the program name. Bytes that are not
UTF-8 become U+FFFD (args_os keeps them).
args_osSource
pub fn args_os() -> List<RawStr> The arguments as raw OS bytes.
varSource
pub fn var(name: str) throws (VarError) -> str name's value; NotPresent when unset, NotUnicode when not UTF-8.
var_osSource
pub fn var_os(name: str) -> RawStr | none name's value as raw bytes, or none.
set_varSource
pub unsafe fn set_var(name: str, value: str) throws (IoError) Set name to value for this process and the children it spawns
later (Command::env sets one child's alone). Throws InvalidInput
for a name that is empty or holds = or NUL, or a value holding NUL;
else the OS error.
Safety: no other thread may read or write the environment during the
call, other than through this module (C code calling getenv, for
one). Sound in a single-threaded program, and on Windows.
remove_varSource
pub unsafe fn remove_var(name: str) throws (IoError) Unset name (unset already is not an error); throws as set_var.
Safety: as set_var.
varsSource
pub const fn vars() -> Vars Every (name, value) pair, a snapshot taken lazily as it goes, in
the order the OS keeps the environment (not sorted). Bytes that are
not UTF-8 become U+FFFD (vars_os keeps them).
vars_osSource
pub const fn vars_os() -> VarsOs vars as raw OS bytes (names and values need not be UTF-8).
current_dirSource
pub fn current_dir() throws (IoError) -> Path The working directory; throws the OS error (NotFound once it was
removed) or InvalidData if its path is not UTF-8. WASI has none of
its own: a program starts in $PWD when that is absolute and the
host lets it in (nabi run and nabi test pass the host's), else
in /.
set_current_dirSource
pub fn set_current_dir<P: AsPath>(path: P) throws (IoError) Make path the working directory; throws the OS error (NotFound,
PermissionDenied, ...).
home_dirSource
pub fn home_dir() -> Path | none $HOME (Windows: %USERPROFILE%); none when it is unset or not
UTF-8.
temp_dirSource
pub fn temp_dir() -> Path $TMPDIR, else /tmp. Windows: the first set of %TMP%, %TEMP%,
%USERPROFILE%, %SystemRoot% (as GetTempPath2W).
config_dirSource
pub fn config_dir() -> Path | none Per-user configuration: ~/Library/Application Support,
%APPDATA%, or $XDG_CONFIG_HOME / ~/.config.
cache_dirSource
pub fn cache_dir() -> Path | none Per-user cache: ~/Library/Caches, %LOCALAPPDATA%, or
$XDG_CACHE_HOME / ~/.cache.
data_dirSource
pub fn data_dir() -> Path | none Per-user data: ~/Library/Application Support, %APPDATA%, or
$XDG_DATA_HOME / ~/.local/share.
split_pathsSource
pub const fn split_paths(paths: str) -> List<Path> paths split on : (Windows: ;, " quoting dropped); empty
pieces dropped.
join_pathsSource
pub const fn join_paths<I: IntoIterator<Item = P>, P: AsPath>(paths: I) throws (JoinPathsError) -> str paths joined with : (Windows: ;), the PATH form; throws if one
holds the separator (or, on Windows, a ").
current_exeSource
pub fn current_exe() throws (IoError) -> Path The running executable's absolute path.