Skip to content

Environment Variables and Platform

Environment variables

env.get(key) returns str | nil — combine it with ?? to provide a default without any extra conditional. env.set and env.remove allow mutating the current process environment (useful for propagating it to subprocesses or in tests):

Sets ZOLO_DEMO_VAR, reads it with fallback ??, then removes it to show that env.get returns nil when the key does not exist.

02-env-vars.zolo
Playground
// Feature: environment variables — `env.get`, `env.set`, `env.remove`
// Syntax: `env.get(key)` returns `str | nil`; `env.set(key, val)` mutates;
// `env.remove(key)` deletes; `env.all()` returns the full map.
// When to use: read config (`PORT`, `DATABASE_URL`), feature flags, secrets.

use std::env

// Set a var just for the demo.
env.set("ZOLO_DEMO_VAR", "hello-from-zolo")

// `env.get` returns nil if absent — combine with `??`.
let val = env.get("ZOLO_DEMO_VAR") ?? "(not set)"
print("ZOLO_DEMO_VAR = {val}")

// expected: ZOLO_DEMO_VAR = hello-from-zolo

// Typical config pattern: read with a default fallback.
let port = env.get("PORT") ?? "3000"
let log_level = env.get("LOG_LEVEL") ?? "info"
print("port: {port}, level: {log_level}")

// Clear it once we no longer need it.
env.remove("ZOLO_DEMO_VAR")
let removed = env.get("ZOLO_DEMO_VAR") ?? "(removed)"
print("after remove: {removed}")
// expected: after remove: (removed)

Platform detection

env.os() returns "windows", "linux", or "macos"; env.arch() returns "x64", "arm64", etc.; env.home_dir() returns the current user's home directory or nil when unavailable. Use these values to branch platform-dependent behavior:

Prints OS, architecture, and home, then demonstrates a list_cmd() function that picks dir or ls based on the detected OS.

03-os-and-arch.zolo
Playground
// Feature: platform detection — `env.os()`, `env.arch()`, `env.home_dir()`
// Syntax: each is a no-arg function. `os()` returns `"windows"`,
// `"linux"`, or `"macos"`. `arch()` returns `"x64"`, `"arm64"`, etc.
// When to use: branching by platform (paths, different commands),
// multi-arch builds, telemetry.

use std::env

let os_name = env.os()
let arch = env.arch()
let home = env.home_dir() ?? "(home unavailable)"

print("OS:   {os_name}")
print("ARCH: {arch}")
print("HOME: {home}")

// expected (Windows x64):
//   OS:   windows
//   ARCH: x64
//   HOME: C:\Users\<...>

// Pattern: a different command per OS.
fn list_cmd() -> str {
  if env.os() == "windows" {
    return "dir"
  }
  return "ls"
}

print("listing command on this platform: {list_cmd()}")

Challenge

Combine env.os() and env.arch() to build a platform identifier such as "linux-arm64" and print a different message for each combination.

enespt-br