docs

v0.1.2 문서를 보고 있습니다. 이 버전은 더 이상 지원하지 않습니다. 최신 릴리스: v0.1.3 →

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 RawStr

Bytes from the OS that need not be UTF-8 (args_os, var_os).

  • pub const fn len(self) -> usize§

    Source

    Byte length.

  • pub const fn is_empty(self) -> bool§

    Source

    true when there are no bytes.

  • pub const fn to_str(self) throws (Utf8Error) -> str§

    Source

    The bytes as text; throws if they are not UTF-8.

  • pub fn to_str_lossy(self) -> str§

    Source

    The text with each ill-formed part as U+FFFD (Windows: each lone surrogate).

  • pub const fn as_bytes(self) -> Slice<u8>§

    Source

    The bytes, as a read-only view (no copy).

  • pub fn from_wide(wide: Slice<u16>) -> RawStr§

    Source

    UTF-16 units from the OS, lone surrogates allowed.

  • pub fn as_wide(self) -> Slice<u16>§

    Source

    The UTF-16 units, as a view of a list made for it.

Trait implementations 1

VarsSource

pub struct Vars

The iterator vars returns.

Trait implementations 2

VarsOsSource

pub struct VarsOs

The iterator vars_os returns.

Trait implementations 2

JoinPathsErrorSource

pub struct JoinPathsError

A join_paths entry that holds the separator (or, on Windows, a ").

  • pub const fn path(self) -> str§

    Source

    The path that cannot be joined.

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for JoinPathsError
  • impl Debug for JoinPathsError
  • impl Error for JoinPathsError

Enums

VarErrorSource

pub enum VarError

Why var gave no str.

  • NotPresent§

    Source

    The variable does not exist.

  • NotUnicode§

    Source

    Its value is not UTF-8 (read it with var_os).

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for VarError
  • impl Debug for VarError
  • impl Error for VarError

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.