Last active
October 24, 2020 20:58
-
-
Save yuyawata/9b788d09f40afa43846762953c579c9d to your computer and use it in GitHub Desktop.
Rust envconfig example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rust-envconfig-sample" | |
version = "0.1.0" | |
authors = ["Yutaka Yawata"] | |
edition = "2018" | |
[dependencies] | |
envconfig = "0.5.0" | |
envconfig_derive = "0.5.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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