How to Read Environment Variables in Rust

Read environment variables in Rust using std::env::var to retrieve values or handle missing keys.

Use std::env::var to read an environment variable by name, which returns a Result containing the value or an error if missing.

use std::env;

fn main() {
    let key = "MY_VAR";
    match env::var(key) {
        Ok(val) => println!("Value: {}", val),
        Err(_) => println!("Variable not set"),
    }
}