Macro env_var

Source
macro_rules! env_var {
    ($key: expr) => { ... };
    ($key: expr, Vec<$type: ty>) => { ... };
    ($key: expr, $type: ty) => { ... };
}
Expand description

Read environment variable using a key.

  • When only (key) is passed, returns the value as a String.

§Example

let x:String = env_var!("keyA"); // returns `valueA` as a String.
assert_eq!(x, "valueA".to_string());
  • When the key and value return type is passed, the environment variable is read for the key and the value is parsed into the type passed as argument.

§Example

let x:u32 = env_var!("keyA", u32); // returns `20` as a String.
assert_eq!(x, 20);
  • Special scenario to convert the string value to Vector. When the key and value return type is passed as Vec<type>
    • the environment variable is read for the key.
    • the string value returned is split on , to create a Vec.
    • each value of the vec is parsed into the type passed as argument.

§Example

let x:Vec<String> = env_var!("keyA", Vec<String>); // holds value `"test,1,2,3"` returns `["test", "1", "2", "3"]` as a String.
assert_eq!(x[0], "test".to_string());