read-dotenv-files

This repository contains small scripts as well as instructions to read .env files in several languages.

View the Project on GitHub Baelfire18/read-dotenv-files

Rust

  1. You’ll need a working Rust installation.
  2. You’ll need the dotenv crate, installable by adding it as a dependency on your Cargo.toml file:
[dependencies]
dotenv = "0.15.0"

Having done this, you can take use of the dotenv() function to load the variables from .dotenv and then use the env::var function to access them normally:

extern crate dotenv;

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

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

    let db_name = env::var("DB_NAME").unwrap();
    println!("{}", db_name);
}