Skip to content

Instantly share code, notes, and snippets.

@yuyawata
Last active October 24, 2020 20:58
Show Gist options
  • Save yuyawata/9b788d09f40afa43846762953c579c9d to your computer and use it in GitHub Desktop.
Save yuyawata/9b788d09f40afa43846762953c579c9d to your computer and use it in GitHub Desktop.
Rust envconfig example
[package]
name = "rust-envconfig-sample"
version = "0.1.0"
authors = ["Yutaka Yawata"]
edition = "2018"
[dependencies]
envconfig = "0.5.0"
envconfig_derive = "0.5.0"
use envconfig_derive::Envconfig;
use envconfig::Envconfig;
use std::process;
#[derive(Envconfig, Debug)]
struct Config {
#[envconfig(from = "HOST")]
host: String,
#[envconfig(from = "PORT", default = "8080")]
port: u16,
}
fn main() {
// HOST=myserver
// PORT=8080
let config = match Config::init() {
Ok(val) => val,
Err(err) => {
println!("{}",err);
process::exit(1);
}
};
println!("{:#?}", config);
assert_eq!(config.host, "myserver");
assert_eq!(config.port, 8080);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment