2 unstable releases

0.1.0 Nov 24, 2022
0.0.1 Apr 28, 2022

#487 in Configuration

Download history 1007/week @ 2023-12-15 159/week @ 2023-12-22 811/week @ 2023-12-29 1190/week @ 2024-01-05 640/week @ 2024-01-12 1074/week @ 2024-01-19 1148/week @ 2024-01-26 819/week @ 2024-02-02 931/week @ 2024-02-09 951/week @ 2024-02-16 970/week @ 2024-02-23 1446/week @ 2024-03-01 1027/week @ 2024-03-08 966/week @ 2024-03-15 734/week @ 2024-03-22 748/week @ 2024-03-29

3,836 downloads per month
Used in 2 crates

MIT license

13KB
279 lines

dotenvs

crates.io Released API docs MIT licensed

A correct dotenv library, which supports:

  • Multiline values
  • Variable substitution

Parsing rules

  • BASIC=basic
  • empty lines are skipped
  • lines beginning with # are treated as comments
  • # marks the beginning of a comment (unless when the value is wrapped in quotes)
  • empty values become empty strings
  • inner quotes are maintained
  • whitespace is removed from both ends of unquoted values
  • single and double quoted values are escaped
  • single and double quoted values maintain whitespace from both ends
  • double quoted values expand new lines (MULTILINE="new\nline" becomes
    MULTILINE: "new
    line"
    

Expanding rules

  • $KEY will expand any env with the name KEY
  • ${KEY} will expand any env with the name KEY
  • \$KEY will escape the $KEY rather than expand
  • ${KEY:-default} will first attempt to expand any env with the name KEY. If not one, then it will return default

Usage

The easiest and most common usage consists on calling load when the application starts, which will load environment variables from a file named .env in the current directory or any of its parents.

If you need more control about the file name or its location, you can use the from_filename, from_path or from_read.

Examples

A .env file looks like this:

# a comment, will be ignored
REDIS_ADDRESS=localhost:6379
MEANING_OF_LIFE=42

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:

fn main() {
    for (key, value) in dotenv::vars() {
        println!("{}: {}", key, value);
    }
}

LICENSE

MIT

Dependencies

~1MB
~17K SLoC