Read environment variables with std::env::var and command-line arguments with std::env::args in Rust.
Use std::env::var to read a specific environment variable or std::env::args to access command-line arguments passed to your program.
use std::env;
fn main() {
if let Ok(val) = env::var("MY_VAR") {
println!("Value: {val}");
}
let args: Vec<String> = env::args().collect();
println!("Args: {args:?}");
}
std::env lets your Rust program read settings or instructions from the outside world without changing the code. Think of it like a recipe that checks a sticky note on the counter for the amount of salt to use, rather than having the amount written permanently in the book. You use this to configure your app differently for testing versus production.