Fast .env file parser with variable interpolation, multi-file layering, and type-safe loading
cargo add philiprehberger-dotenvFast .env file parser with variable interpolation, multi-file layering, and type-safe loading
[dependencies]
philiprehberger-dotenv = "0.3.0"
use philiprehberger_dotenv::DotEnv;
// Load .env from current directory
let env = DotEnv::load()?;
// Type-safe access
let port: u16 = env.get_as("PORT")?;
let debug: bool = env.get_bool("DEBUG")?;
let name = env.get_or("APP_NAME", "my-app");
let timeout: u64 = env.get_or_default("TIMEOUT_SECS", 30);
// Validate required variables
env.require(&["DATABASE_URL", "SECRET_KEY"])?;
| Function / Type | Description |
|---|---|
DotEnv::load() | Load .env from current directory |
DotEnv::load_from(path) | Load from specific file |
DotEnv::load_layered(paths) | Load multiple files with priority |
DotEnv::from_string(content) | Parse .env-formatted content from a string |
.get(key) | Get raw string value |
.get_or(key, default) | Get with string default |
.get_or_default::<T>(key, default) | Get with typed default (returns default on missing or parse failure) |
.get_as::<T>(key) | Type-safe parsing |
.get_bool(key) | Parse boolean values |
.get_list(key, sep) | Split value into list |
.require(keys) | Validate required variables |
.set(key, value) | Programmatically set or overwrite a key |
.merge(other) | Merge another DotEnv; keys from other override existing |
.apply() | Set vars into process environment |
.len() | Number of loaded variables |
.is_empty() | True if no variables are loaded |
load_and_apply() | Load .env and apply to process |
cargo test
cargo clippy -- -D warnings
If you find this project useful: