Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

adotenv

adotenv is a literal, lossless .env parser and editing library for Rust.

It is built for linters, formatters, editors, scanners and codebase maintenance tools that need to inspect source exactly as written.

What it provides

  • Literal values with no implicit expansion or command execution.
  • Byte-for-byte source retention, including comments, invalid statements, BOM and mixed line endings.
  • Precise source spans and checked UTF-8, scalar and UTF-16 positions.
  • Verified edits, formatting, sorting, comparison and example generation.
  • Five syntax presets and explicit reference resolution.
  • Optional file loading with values, origins and reports.
  • No required runtime dependencies and a no_std build with alloc.

Install

[dependencies]
adotenv = "0.10.4"

Enable optional features as needed:

adotenv = { version = "0.10.4", features = ["load", "serde"] }

Quick start

#![allow(unused)]
fn main() {
let text = "# app\nexport PORT=8080\nURL=\"${HOST}/api\"\n";
let doc = adotenv::parse_strict(text).unwrap();

assert_eq!(doc.get("PORT"), Some("8080"));
assert_eq!(doc.get("URL"), Some("${HOST}/api"));
assert_eq!(doc.to_string(), text);

let edited = doc.set("PORT", "9090").unwrap();
assert_eq!(
    adotenv::parse_strict(edited.as_str()).unwrap().get("PORT"),
    Some("9090")
);
}

When only final values are needed, use the values-only parser:

#![allow(unused)]
fn main() {
let values = adotenv::parse_values("A=1\nA=2\nB=3\n").unwrap();
assert_eq!(values.get("A").map(String::as_str), Some("2"));
}

Documentation

Source code and issues: github.com/asamarts/adotenv.