Crate dotenv_build[][src]

Expand description

Overview

This crate allows you to load .env files in your compilation step. It is built to be used in your build.rs file.

Usage

  1. Ensure you have build scripts enabled via the build configuration in your Cargo.toml
  2. Add dotenv-build as a build dependency
  3. Create a build.rs file that uses dotenv-build to generate cargo: instructions.
  4. Use the env! or option_env! macro in your code

Cargo.toml

[package]
#..
build = "build.rs"

[dependencies]
#..

[build-dependencies]
dotenv-build = "0.1"

build.rs

// in build.rs
fn main() {
    dotenv_build::output(dotenv_build::Config::default()).unwrap();
}

Use in code

println!("Your environment variable in .env: {}", env!("TEST_VARIABLE"));

Configuration

Read more about the available options here: Config

let config = dotenv_build::Config {
  recursive_search: false,
  fail_if_missing_dotenv: true,
   ..Default::default()
};

dotenv_build::output(config).unwrap();

Structs

Config for output

Functions

Outputs the necessary build.rs instructions.