Logo

Programming-Idioms

History of Idiom 205 > diff from v25 to v26

Edit summary for version 26 by programming-idioms.org:
[Rust] Now assigns to var foo, instead of printing

Version 25

2020-05-22, 10:14:07

Version 26

2020-06-08, 22:10:08

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

Variables
foo
Variables
foo
Extra Keywords
envvar os system
Extra Keywords
envvar os system
Imports
use std::env;
Imports
use std::env;
Code
let key = "FOO";
match env::var(key) {
    Ok(val) => println!("{}: {:?}", key, val),
    Err(e) => println!("couldn't interpret {}: {}", key, e),
}
Code
let foo;
match env::var("FOO") {
    Ok(val) => foo = val,
    Err(_e) => foo = "none".to_string(),
}
Doc URL
https://doc.rust-lang.org/1.31.0/std/env/fn.var.html
Doc URL
https://doc.rust-lang.org/1.31.0/std/env/fn.var.html
Origin
https://doc.rust-lang.org/1.31.0/std/env/fn.var.html
Origin
https://doc.rust-lang.org/1.31.0/std/env/fn.var.html