liveBook · Manning

Environment variable

As explained in Learn Rust in a Month of Lunches

An environment variable is a named operating-system or process setting represented as a string key and string value.

An environment variable is a named operating-system or process setting stored as a string key and string value. Programs use these values for overall configuration rather than for arguments typed on the command line. section 18.2.3 section 18.5

In Rust, the standard library lets a program inspect and change these settings:

  • Read one value with std::env::var("RUST_BACKTRACE"); an unset variable produces Err(NotPresent). section 14.2.2
  • List all variables with std::env::vars(), which yields (String, String) key-value pairs. section 18.2.3
  • Set a value while the program runs with std::env::set_var("RUST_BACKTRACE", "1"). section 14.2.2
  • Use configuration fallbacks: check RUST_LOG first, then choose a logging level from LOGGER_URL when needed. section 18.2.3
fn main() {
    for (key, value) in std::env::vars() {
        println!("{key}: {value}");
    }
}

The listing makes the key-value shape visible by printing every environment variable available to a Rust process. section 18.2.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains string key-value storage and Rust accessors, then demonstrates reading, listing, setting, and fallback configuration.
Examples
Uses RUST_BACKTRACE, RUST_LOG, LOGGER_URL, complete environment listings, and runtime key-value rewriting.
What it assumes
Assumes basic Rust syntax while distinguishing environment variables from command-line arguments.
What you can do after
Operate/configure it by reading, listing, setting, and selecting Rust program behavior from environment values.
How it connects
Connects environment variables with command-line arguments, logging, backtraces, URLs, and operating-system configuration.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.