dotenvor
dotenvor is a small, fast .env parser and loader for Rust.
It focuses on predictable behavior, low dependency overhead, and an ergonomic API (EnvLoader + convenience functions).
Highlights
- Fast parser for common
.envsyntax - Builder-style loader with multi-file precedence
- Built-in multi-environment stack helper (
.convention("development")) - Optional variable substitution (
$VAR,${VAR},${VAR:-fallback}) - Optional upward search for
.envfiles - First-party
dotenvCLI (dotenv run ...) - Process-env or in-memory targets for safer tests
- Quiet/verbose logging controls
Installation
[]
= "0.1"
MSRV
The minimum supported Rust version (MSRV) is Rust 1.88.0+. We don't treat MSRV changes as breaking, it may be changed in any release.
Quick Start
Load into the process environment
use dotenv;
// Looks for ".env" and searches parent directories if needed.
let report = unsafe ;
println!;
# Ok::
dotenv() mutates process-wide state via std::env::set_var and is unsafe,
because callers must guarantee no concurrent process-environment access.
In concurrent code or isolated tests, prefer a loader configured with
.target(TargetEnv::memory()).
Builder API with memory target
use ;
let mut loader = new
.path
.search_upward
.required // skip missing files instead of returning Error::Io
.key_parsing_mode
.substitution_mode
.override_existing
.target;
let report = loader.load?;
let env = loader.target_env.as_memory.expect;
println!;
println!;
# Ok::
Multi-environment stack convention
use ;
let mut loader = new
.convention
.required
.target;
loader.load?;
# Ok::
Convention precedence (highest to lowest):
.env.development.local.env.local.env.development.env
Parse only
use parse_str;
let entries = parse_str?;
assert_eq!;
# Ok::
CLI: run a command with dotenv files
Select files explicitly (repeat -f or use comma-separated paths):
Useful flags:
-o,--override: let file values override existing environment variables-i,--ignore: skip missing files-u,--search-upward: resolve relative files by walking parent directories
Opt in to permissive key parsing
use ;
let entries = parse_str_with_mode?;
assert_eq!;
# Ok::
Implemented Behavior
Parsing
KEY=VALUEpairs- Whitespace trimming around keys and values
- Empty values (
FOO=) - Comments with
#outside quotes - Single quotes, double quotes, and backticks
- Double-quoted escapes:
\n,\r,\t,\\,\" - Optional
exportprefix - Duplicate keys: last value wins
- Reader, string, and bytes parsing APIs
- Multiline quoted values (including PEM-style blocks)
- Strict key mode by default, plus opt-in
KeyParsingMode::Permissive
Loading
- Multi-file loading with deterministic precedence
- Convention helper for environment stacks (
.convention("development")) override_existing(false)by defaultEnvLoaderdefaults toTargetEnv::memory()for process-isolated loads- Process-env loading is available via
unsafe { TargetEnv::process() }and unsafe convenience functions (dotenv,from_path,from_paths,from_filename) - Upward file search support
dotenv()/from_filename(...): upward search enabledEnvLoader: upward search disabled by default (enable with.search_upward(true))
- Missing-file mode
required(true)(default): missing files returnError::Iorequired(false): missing files are skipped silently
- Configurable file decoding via
.encoding(...)Encoding::Utf8(default)Encoding::Latin1(ISO-8859-1)
- CLI command execution (
dotenv run)- Defaults to
.envwhen no file is selected - Accepts
-f/--filefor file selection (repeatable and comma-separated) - Supports
-o/--overrideand-i/--ignore
- Defaults to
Substitution
- Optional mode:
SubstitutionMode::Expand - Expands
$VAR,${VAR}, and${VAR:-fallback}(strict key mode) - Supports chained and forward references
- Falls back to current target environment values when needed
- Treats single-quoted values and escaped dollars (
\$) as literal in expand mode
Logging
.verbose(true)enables loader diagnostics on stderr.quiet(true)suppresses diagnostics
License
Licensed under either of:
- MIT license (LICENSE-MIT)
- Apache License, Version 2.0 (LICENSE-APACHE)
at your option.