beginnerCLI
Environment Variables
Read and use environment variables.
Environment Variables
Read and use environment variables.
Difficulty
Beginner
Code
rust
use std::env;
fn main() {
match env::var("HOME") {
Ok(val) => println!("HOME: {}", val),
Err(e) => println!("not set: {}", e),
}
let port = env::var("PORT").unwrap_or("8080".to_string());
println!("PORT: {}", port);
}Explanation
std::env::var reads environment variables.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the cli category to build a complete understanding of this topic.