dotenv (Rust)

Loads environment variables from a .env file for Rust development and testing

Library
Cargo
v0.15.0
624stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
40/100Fair
Development Activity0
Maintenance20
Community52
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
67/100Good
Architecture65
Code Quality72
Innovation40
Learning Curve92

dotenv is a Rust port of the Ruby dotenv gem: it reads key-value pairs from a .env file in the current directory (or any parent directory) and merges them into the process’s environment variables, so local development and test runs don’t require exporting secrets or config manually. Calling dotenv::dotenv().ok() at the start of main() is the typical integration point, after which code just reads values via the standard std::env APIs.

The workspace also ships dotenv_codegen, which provides a dotenv! macro that behaves like the standard env! macro but reads from a .env file at compile time, and supports bash-style variable substitution ($VAR, ${VAR}) inside the .env file itself. The project has been in maintenance mode since 2020 (last release v0.15.0), with the community-maintained dotenvy fork now recommended for actively developed use, but the original crate remains widely depended upon.

What You Get

  • dotenv() to auto-discover and load a .env file from the current or a parent directory
  • from_filename() / from_path() for loading a specifically named or located env file
  • Iterator variants (dotenv_iter, from_path_iter) for inspecting parsed values without mutating the environment
  • Bash-style $VAR / ${VAR} variable substitution and comment/quoting support in .env parsing
  • A companion dotenv_codegen crate providing a dotenv! compile-time macro analogous to env!
  • An optional cli feature building a standalone dotenv binary for shell use

Common Use Cases

  • Loading local development secrets (database URLs, API keys) from a .env file at the top of main()
  • Configuring integration tests with a .env.test file via from_filename() without touching the shell environment
  • Baking configuration into a binary at compile time using the dotenv! macro instead of a runtime lookup
  • Sharing a .env.example template across a team while keeping actual secrets untracked in git

Under The Hood

Architecture — The workspace splits cleanly into dotenv (the runtime parser/loader in find.rs, parse.rs, iter.rs, errors.rs) and dotenv_codegen/dotenv_codegen_implementation (a proc-macro crate pair implementing the dotenv! macro by shelling out to the same parsing logic at compile time). Tech Stack — Pure Rust, 2018 edition, with almost no dependencies: the core crate only optionally pulls in clap v2 behind a cli feature flag, and tempfile is a dev-only test dependency. Code Quality — Coverage is broad for a small crate: dedicated integration tests exist for filename loading, path loading, child-directory discovery, variable substitution, and iterator behavior (test-variable-substitution.rs, test-child-dir.rs, etc.), tracked with CI and Codecov, though the project has had no commits since mid-2024 and no release since 2020, so it functions as a stable but frozen dependency. API Design — The primary entry point is a single call, dotenv().ok(), with clearly named escape hatches (from_filename, from_path) for non-default cases, keeping the learning curve close to zero and matching the ergonomics of the well-known Ruby/Node dotenv libraries it was modeled on.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search