21 releases (breaking)

✓ Uses Rust 2018 edition

0.15.0 Oct 21, 2019
0.14.1 May 15, 2019
0.13.0 May 30, 2018
0.11.0 Feb 20, 2018
0.2.0 Nov 20, 2014

#1 in Configuration

Download history 20416/week @ 2020-03-16 21230/week @ 2020-03-23 22309/week @ 2020-03-30 19644/week @ 2020-04-06 22271/week @ 2020-04-13 22609/week @ 2020-04-20 22821/week @ 2020-04-27 22155/week @ 2020-05-04 24927/week @ 2020-05-11 22641/week @ 2020-05-18 24820/week @ 2020-05-25 24787/week @ 2020-06-01 29560/week @ 2020-06-08 28257/week @ 2020-06-15 31690/week @ 2020-06-22 27884/week @ 2020-06-29

85,671 downloads per month
Used in 261 crates (222 directly)

MIT license

833 lines


Build Status codecov Crates.io

Achtung! This is a v0.* version! Expect bugs and issues all around. Submitting pull requests and issues is highly encouraged!

Quoting bkeepers/dotenv:

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.

This library is meant to be used on development or testing environments in which setting environment variables is not practical. It loads environment variables from a .env file, if available, and mashes those with the actual environment variables provided by the operative system.


The easiest and most common usage consists on calling dotenv::dotenv when the application starts, which will load environment variables from a file named .env in the current directory or any of its parents; after that, you can just call the environment-related method you need as provided by std::os.

If you need finer control about the name of the file or its location, you can use the from_filename and from_path methods provided by the crate.

dotenv_codegen provides the dotenv! macro, which behaves identically to env!, but first tries to load a .env file at compile time.


A .env file looks like this:

# a comment, will be ignored

You can optionally prefix each line with the word export, which will conveniently allow you to source the whole file on your shell.

A sample project using Dotenv would look like this:

extern crate dotenv;

use dotenv::dotenv;
use std::env;

fn main() {

    for (key, value) in env::vars() {
        println!("{}: {}", key, value);

Variable substitution

It's possible to reuse variables in the .env file using $VARIABLE syntax. The syntax and rules are similar to bash ones, here's the example:


# Non-existing values are replaced with an empty string
RESULT=$NOPE #value: '' (empty string)

# All the letters after $ symbol are treated as the variable name to replace
RESULT=$VAR #value: 'one'

# Double quotes do not affect the substitution
RESULT="$VAR" #value: 'one'

# Different syntax, same result 
RESULT=${VAR} #value: 'one'

# Curly braces are useful in cases when we need to use a variable with non-alphanumeric name
RESULT=$VAR_2 #value: 'one_2' since $ with no curly braces stops after first non-alphanumeric symbol 
RESULT=${VAR_2} #value: 'two'

# The replacement can be escaped with either single quotes or a backslash:
RESULT='$VAR' #value: '$VAR'
RESULT=\$VAR #value: '$VAR'

# Environment variables are used in the substutution and always override the local variables
RESULT=$PATH #value: the contents of the $PATH environment variable
PATH="My local variable value"
RESULT=$PATH #value: the contents of the $PATH environment variable, even though the local variable is defined

Dotenv will parse the file, substituting the variables the way it's described in the comments.

Using the dotenv! macro

Add dotenv_codegen to your dependencies, and add the following to the top of your crate:

extern crate dotenv_codegen;

Then, in your crate:

fn main() {
  println!("{}", dotenv!("MEANING_OF_LIFE"));