Skip to content

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.env

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.

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 / windows
println(env.temp_dir()) // always succeeds
for arg in env.args() { println(arg) }
  • get/home_dir/user return Option (.none when absent) — total.
  • temp_dir/os/arch/args — total.
  • var/set/cwd/set_cwd/exe_path return Result<_, str>.
  • env.set/env.remove are process-wide (POSIX-like) — affect the whole program.
  • env.remove is a total no-op when the key is absent.
  • env.args() returns script args; use std.args for flag parsing.
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)
}

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.

  • 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().
  • join semantics: b wins when absolute — join(dir, "/f.txt") returns /f.txt.

14 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.env.arch arch() -> str Architecture string
std.env.args args() -> [str] Script arguments
std.env.cwd cwd() -> Result<str, str> Working directory (.err on failure)
std.env.exe_path exe_path() -> Result<str, str> Absolute path of this binary
std.env.get get(key: str) -> Option<str> Value or .none when absent
std.env.home_dir home_dir() -> Option<str> HOME / USERPROFILE
std.env.os os() -> str “linux” / “macos” / “windows”
std.env.remove remove(key: str) -> unit Remove (total no-op when absent)
std.env.set set(key: str, val: str) -> Result<unit, str> Set (.err on bad key)
std.env.set_cwd set_cwd(path: str) -> Result<unit, str> Change directory (.err on failure)
std.env.temp_dir temp_dir() -> str System temp dir (total)
std.env.user user() -> Option<str> USER/LOGNAME / USERNAME
std.env.var var(name: str) -> Result<str, str> Value or .err
std.env.vars vars() -> {str: str} All variables, sorted by key
  • 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