Skip to content
CryoCryo home
StdlibSystem

env

The process environment: command-line arguments, environment variables, the working directory, and exit.

import std::env;

function main() -> i32 {
    mut argv: Array<String> = args();
    if (argv.length() < 2) {
        eprintln("usage: tool <path>");
        return 1;
    }
    return 0;
}

Everything here lives directly in the module, so a single import std::env; brings it all in.

Arguments

args() -> Array<String> returns the argument vector as owned UTF-8 strings, the first element being the program name. It returns an empty array if the runtime has not published argv yet.

Argv comes from the C runtime: the generated main(argc, argv) publishes it before your code runs, so args() sees the real vector.

Environment variables

FunctionReturnsNotes
var(name: Str)Option<String>None when unset. The result owns a copy.
vars()Array<Pair<String, String>>Every binding as a key/value pair.
set_var(name: Str, value: Str)booleansetenv(3) on POSIX, _putenv_s on Windows — both overwrite.
remove_var(name: Str)booleanOn Windows this is _putenv_s(name, "").

vars() reflects the environment inherited at process start — the vector the C runtime handed main, the same view as /proc/self/environ. It does not track later set_var and remove_var calls, because setenv and unsetenv build a fresh environ array and leave the original in place. Use var(name) to read a binding you have changed.

Working directory and executable

FunctionReturnsNotes
current_dir()Option<String>None if the path could not be read — over 4096 bytes, or a parent removed or unreadable.
set_current_dir(path: Str)booleanAffects the whole process, not just the calling thread.
current_exe()Option<String>The running executable's absolute path.

There is no portable primitive for current_exe, so the dispatch is explicit per target rather than hidden behind a uniform-looking libc wrapper: Unix reads the /proc/self/exe symlink, Windows asks kernel32 for the module path.

Exit

process_exit(code: i32) -> void terminates the process with code. It does not return, and anything sitting in an unflushed buffered writer is lost — flush explicitly first. See BufWriter.