Skip to main content

parse_dotenv

Function parse_dotenv 

Source
pub fn parse_dotenv(path: &Path) -> SafeResult<HashMap<String, String>>
Expand description

Parse a .env file into a HashMap. Handles # comments, blank lines, Parse a .env-style file into a key→value map.

Handles the following line forms (and their whitespace-padded variants):

  • KEY=VALUE (POSIX / dotenv)
  • KEY="quoted" / KEY='q' (quoted values, quotes are stripped)
  • export KEY=VALUE (bash/zsh source-able)
  • $KEY = VALUE (PowerShell variable style)
  • Lines starting with # (comments, skipped)
  • Lines without = (free-form text / section headers, silently skipped)

The parser is intentionally lenient: real-world .env files often contain freeform notes or section headers mixed with assignments.

Two-pass parsing is used so that intra-file $VAR references resolve correctly even when the referenced variable is defined earlier in the same file (e.g. export ARM_CLIENT_ID="..." followed by client_id="$ARM_CLIENT_ID"). Resolution order: process environment first, then values from this file.