1 unstable release
Uses new Rust 2024
| new 0.1.0 | Nov 15, 2025 |
|---|
#3 in #globals
7KB
116 lines
Allows you to read environment variables and .env files at runtime directly in your crate using static variables.
Example
Please ensure you are in the lazy_env/examples directory, as a .env file is provided there. Alternatively, you can create a new .env file in your current working directory and copy-paste the following content into it.
# .env
PARSE_ADDR=192.168.1.1:80
MUST_PROVIDE=provided
ORIGIN_NAME=114514
# main.rs
use std::net::SocketAddr;
use lazy_env::lazy_env;
lazy_env! {
pub static ref PARSE_ADDR: SocketAddr = ([127, 0, 0, 1], 8080).into();
pub static ref MUST_PROVIDE: String = panic!("MUST_PROVIDE is not provided");
static ref USE_ALIAS(concat!("ORIGIN", "_", "NAME")): u32 = 123;
static ref USE_DEFAULT: bool = true;
}
fn main() {
env_logger::builder().is_test(true).init();
if lazy_env::dotenvy::dotenv().is_err() {
panic!(
"Please ensure that your current working directory contains \
a .env file in its recursive parent directories"
);
}
assert_eq!(
(&*PARSE_ADDR, &*MUST_PROVIDE, &*USE_ALIAS, &*USE_DEFAULT),
(
&SocketAddr::from(([192, 168, 1, 1], 80)),
&"provided".to_string(),
&114514,
&true,
)
);
}
Dependencies
~150KB