#dotenv #environment #variable #env


A small, zero-dependency dotenv implementation

5 releases

0.1.4 Dec 28, 2020
0.1.3 Aug 21, 2020
0.1.2 Aug 20, 2020
0.1.1 Aug 19, 2020
0.1.0 Aug 19, 2020

#431 in Configuration

Download history 28/week @ 2023-01-23 36/week @ 2023-01-30 18/week @ 2023-02-06 27/week @ 2023-02-13 17/week @ 2023-02-20 8/week @ 2023-02-27 19/week @ 2023-03-06 33/week @ 2023-03-13 25/week @ 2023-03-20 21/week @ 2023-03-27 16/week @ 2023-04-03 21/week @ 2023-04-10 7/week @ 2023-04-17 16/week @ 2023-04-24 11/week @ 2023-05-01 22/week @ 2023-05-08

61 downloads per month


416 lines


A simple, zero-dependency dotenv implementation for Rust.


This library exposes two similar functions. The first is loadenv::load(), which looks for a file named .env in the current directory and loads its contents into the environment. The second is loadenv::load_buf(), which takes a BufRead and loads its contents into the environment. This function can be used to load .env files from custom locations or from other data structures like &[u8].

Most of the time, load() should be enough for your program.

Variables that are already defined in the environment will not be replaced as per this section on the dotenv project page.

File Format

# Example .env file
# Comments begin with `#` and must be on their own line
# Pairs are in the form `KEY = value`
PASSWORD = k4+5F_Sa9x%LA&Zy

# `=` is optional for empty values

# whitespace around `=` is optional
BOP = baz

A .env file consists of zero or more key-value pairs in the form KEY = value. Whitespace before and after the = is ignored (including whitespace at the beginning and end of the line).

In addition, the following rules apply to all lines:

  • Empty lines are ignored.
  • Lines that begin with # are comments and are ignored. Note that comments must be on their own line.
  • KEY may only contain 0-9, A-Z and _.
  • value may be any value.

A more detailed example can be found in .env in the root of this project.


Load the example .env file and print out the new environment.

fn main() {

    for (k, v) in std::env::vars() {
        println!("{} = '{}'", k, v);

Load a .env from a string.

fn main() {
    let dotenv = "FOO=bar\nBOP = baz \n# Comment\n";

    for (k, v) in std::env::vars() {
        println!("{} = '{}'", k, v);

Running Tests

Tests must be run in single-threaded mode using cargo test -- --test-threads=1. Otherwise, testing will be unreliable because many std::env methods are not thread-safe. See this page for more info.

No runtime deps