18 releases (breaking)

0.13.0 May 30, 2018
0.11.0 Feb 20, 2018
0.10.1 Jul 3, 2017
0.10.0 Mar 24, 2017
0.2.0 Nov 20, 2014

#3 in Configuration

Download history 3155/week @ 2018-11-18 3166/week @ 2018-11-25 3642/week @ 2018-12-02 3951/week @ 2018-12-09 3753/week @ 2018-12-16 3284/week @ 2018-12-23 3933/week @ 2018-12-30 5496/week @ 2019-01-06 4238/week @ 2019-01-13 4219/week @ 2019-01-20 4707/week @ 2019-01-27 5295/week @ 2019-02-03 4898/week @ 2019-02-10 6057/week @ 2019-02-17 5891/week @ 2019-02-24

9,275 downloads per month
Used in 109 crates (100 directly)

MIT license

420 lines

rust-dotenv Build Status

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);

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"));


~57K SLoC