Skip to content

Commit cea4deb

Browse files
Merge pull request #392 from matthiasbeyer/add-static-env-example
Add simple example using lazy_static
2 parents f4c1193 + fec6a4a commit cea4deb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

examples/static_env.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use config::Config;
2+
3+
lazy_static::lazy_static! {
4+
#[derive(Debug)]
5+
pub static ref CONFIG: Config = Config::builder()
6+
.add_source(config::Environment::with_prefix("APP_NAME").separator("_"))
7+
.build()
8+
.unwrap();
9+
}
10+
11+
/// Get a configuration value from the static configuration object
12+
pub fn get<'a, T: serde::Deserialize<'a>>(key: &str) -> T {
13+
// You shouldn't probably do it like that and actually handle that error that might happen
14+
// here, but for the sake of simplicity, we do it like this here
15+
CONFIG.get::<T>(key).unwrap()
16+
}
17+
18+
fn main() {
19+
println!("{:?}", get::<String>("foo"));
20+
}

0 commit comments

Comments
 (0)