expression

Read an optional environment setting

Environment settings belong to the process, not the source code. Match missing values separately from invalid Unicode; validate a present value even if a default exists. The runner injects a synthetic setting at process launch, avoiding mutation of the running environment. Fixture: synthetic environment {'BYTEMIND_DEMO_LIMIT': '7'}. Expected stdout (not executed): limit: 7 Expected stderr: (empty) Expected exit: 0 Common mistake: Using unwrap_or to treat a malformed supplied value as if it were absent. This fixture uses assertions for controlled inputs; a real CLI returns a diagnostic.

Formal Syntax & Structure

rust syntaxrust
std::env::var("BYTEMIND_DEMO_LIMIT")

Working Example

Example Snippetrust
fn main() {    use std::env::{self, VarError};    let limit = match env::var("BYTEMIND_DEMO_LIMIT") {        Ok(raw) => raw.parse::<u16>().expect("invalid demo limit"),        Err(VarError::NotPresent) => 10,        Err(VarError::NotUnicode(_)) => panic!("limit must be Unicode"),    };    assert!((1..=100).contains(&limit));    println!("limit: {limit}");}
Rust Reference