Skip to content
CryoCryo home
StdlibSystem

env

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

env is a single-file module with no sub-modules; everything lives directly in it, so one import std::env; brings it all in.

import std::env;

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

Arguments

args() 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 through set_args before your code runs, so args() sees the real vector. set_args and set_env are the runtime's hooks and are not for user code.

function args() -> Array<String>;

Program arguments as UTF-8 strings. The first element is the program name. Returns an empty array if the runtime hasn't published argv yet. Panics on allocation failure.

Environment variables

FunctionReturnsNotes
var(name)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, value)booleansetenv(3) on POSIX, _putenv_s on Windows — both overwrite.
remove_var(name)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.

function var(name: Str) -> Option<String>;

Read the environment variable name. Returns None if unset. The returned String owns a copy of the value.

function vars() -> Array<Pair<String, String>>;

The process environment as (key, value) pairs, in the order the C runtime laid them out. Each pair owns copies of its key and value Strings. Returns an empty array if the runtime hasn't published the environment yet (a freestanding entry that skips the generated main prologue). Panics on allocation failure.

function set_var(name: Str, value: Str) -> boolean;

Set name = value, overwriting any existing binding. Returns true if the underlying call succeeded.

function remove_var(name: Str) -> boolean;

Remove the binding for name. Returns true if the underlying call succeeded. On Windows the documented way to delete a binding is _putenv_s(name, "").

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

function current_dir() -> Option<String>;

The process's current working directory as an owned String. Returns None if the path could not be read (e.g. it exceeds the 4096-byte buffer, or a parent component was removed/unreadable).

function set_current_dir(path: Str) -> boolean;

Change the process's current working directory to path. Returns true on success. This affects the whole process, not just the calling thread.

function current_exe() -> Option<String>;

The running executable's absolute path as an owned String, or None if it couldn't be resolved (unreadable, or longer than the 4096-byte buffer).

Exit

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

function process_exit(code: i32) -> void;

Exit the process with code. Does not return; anything written to an unflushed buffered writer is lost unless the writer was explicitly flushed first.