#env-var #dotenv #environment #env #settings

bin+lib dotenv_rs

A dotenv implementation for Rust

5 releases

0.16.1 Mar 23, 2023
0.16.0 Mar 23, 2023
0.15.2 Mar 20, 2023
0.15.1 Mar 18, 2023
0.15.0 Mar 18, 2023

#596 in Configuration

Download history 19/week @ 2023-12-07 40/week @ 2023-12-14 14/week @ 2023-12-21 4/week @ 2023-12-28 1/week @ 2024-01-04 25/week @ 2024-01-18 41/week @ 2024-01-25 13/week @ 2024-02-01 6/week @ 2024-02-08 19/week @ 2024-02-15 21/week @ 2024-02-22 36/week @ 2024-02-29 22/week @ 2024-03-07 7/week @ 2024-03-14

69 downloads per month

MIT license

39KB
929 lines

dotenv-rs

forked from dotenv

A sample project using Dotenv would look like this:

extern crate dotenv_rs;

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

fn main() {
    dotenv().ok();

    for (key, value) in env::vars() {
        println!("{}: {}", key, value);
    }
    dotenv_with_prefix(&String::from("Test")).ok();

    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:


VAR=one
VAR_2=two

# 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:

#[macro_use]
extern crate dotenv_codegen;

Then, in your crate:

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

Dependencies

~72KB