std/env

March 14, 2026 ยท View on GitHub

The std/env module provides cross-platform access to process environment variables.

Overview

  • Key-Value Access: Simple API for getting, setting, and unsetting environment variables.
  • Borrowed or Owned: Choose between get (returns a borrowed C string) and get_dup (returns an owned, heap-allocated String).
  • Cross-platform: Safely abstracts underlying system calls for environment manipulation.

Usage

import "std/env.zc"

fn main() {
    // Setting an environment variable
    Env::set("MY_APP_MODE", "development");

    // Retrieving (Borrowed)
    match Env::get("MY_APP_MODE") {
        Some(val) => println "Mode: {val}",
        None => println "Mode not set"
    }

    // Retrieving (Owned String for RAII)
    match Env::get_dup("HOME") {
        Some(home) => {
             println "Home: {home}";
             // home is freed automatically
        }
        None => println "HOME not found"
    }
}

Enum Definition

enum EnvRes {
    OK,
    ERR,
}

Methods

Access & Query

MethodSignatureDescription
getEnv::get(name: char*) -> Option<char*>Retrieves a borrowed pointer to an environment variable. Do not free.
get_dupEnv::get_dup(name: char*) -> Option<String>Retrieves an environment variable as a new String object.

Modification

MethodSignatureDescription
setEnv::set(name: char*, value: char*) -> EnvResSets or updates an environment variable.
unsetEnv::unset(name: char*) -> EnvResRemoves an environment variable from the current process.