std.env - ZZ
Cross-platform environment + OS identity. get/home_dir/user return Option; temp_dir is total; var/set/cwd/set_cwd/exe_path are fallible (Result).
import std.envPurpose
Section titled “Purpose”std.env reads/sets environment variables, queries the working directory, executable path, home/temp dirs, current user, OS/arch strings, and script args. Process-wide like POSIX getenv/setenv.
Quickstart
Section titled “Quickstart”import std.env
match env.get("HOME") { .some(home) => println("Home: {home}"), .none => println("HOME not set"),}match env.set("APP_MODE", "demo") { .ok(_) => println("set"), .err(e) => println(e),}env.remove("APP_MODE") // total no-op when absent
match env.cwd() { .ok(c) => println(c), .err(e) => println(e),}println(env.os()) // linux / macos / windowsprintln(env.temp_dir()) // always succeeds
for arg in env.args() { println(arg) }Error model
Section titled “Error model”get/home_dir/userreturnOption(.nonewhen absent) — total.temp_dir/os/arch/args— total.var/set/cwd/set_cwd/exe_pathreturnResult<_, str>.
Gotchas
Section titled “Gotchas”env.set/env.removeare process-wide (POSIX-like) — affect the whole program.env.removeis a total no-op when the key is absent.env.args()returns script args; usestd.argsfor flag parsing.
Production patterns
Section titled “Production patterns”import std.env
func require_env(key: str) -> Result<str, str> { return match env.get(key) { .some(v) => .ok(v), .none => .err("env: {key} not set"), }}
func main() -> Result<int, str> { home := require_env("HOME")? println(home) return .ok(0)}Purpose
Section titled “Purpose”std.path resolves, joins, and inspects paths without touching the disk. Separator style follows std.env.os() ("windows" → \, else /). Use for building paths before passing them to std.fs.
Edge cases & gotchas
Section titled “Edge cases & gotchas”- Purely lexical — never checks the filesystem. A path may be “valid” here and still not exist on disk.
..above root clamps;.segments are dropped.- Backslashes accepted on all platforms; canonical separator follows
std.env.os(). joinsemantics:bwins when absolute —join(dir, "/f.txt")returns/f.txt.
API reference
Section titled “API reference”14 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
See also
Section titled “See also”std.args— argv access and the flag parser: booleans, integers, strings and subcommands.std.process— Spawning, waiting on and signalling child processes.std.log— Log levels, sinks and spans for structured logging.std.sys— Infallible machine information: OS, CPU, memory and uptime.std.path— Pure lexical path manipulation: join, split, extension and normalisation.std.cell— Shared mutable slots for lock-free hand-off between green threads.
Full index: API reference · Standard library