Skip to content

Instantly share code, notes, and snippets.

@yuyawata
Created February 28, 2019 05:51
Show Gist options
  • Save yuyawata/42b0dfe10e48016e904976e69f1cbff3 to your computer and use it in GitHub Desktop.
Save yuyawata/42b0dfe10e48016e904976e69f1cbff3 to your computer and use it in GitHub Desktop.
Rust envy example2
use serde_derive::Deserialize;
use envy;
use std::process;
#[derive(Deserialize, Debug)]
struct Config {
host: String,
port: Option<u16>,
numbers: Vec<u64>,
}
fn main() {
// APP_HOST=myserver
// PORT=8080
// APP_NUMBERS=1,2,3
let config = match envy::prefixed("APP_").from_env::<Config>() {
Ok(val) => val,
Err(err) => {
println!("{}", err);
process::exit(1);
}
};
println!("{:#?}", config);
assert_eq!(config.host, "myserver");
assert_eq!(config.port, None);
assert_eq!(config.numbers, vec![1,2,3]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment