Skip to main content

load

Function load 

Source
pub fn load() -> Result<(), Error>
Expand description

Loads the .env file from the current working directory.

Each key-value pair found in the file is set as an environment variable for the current process, subject to these rules:

  1. A variable already present in the environment is not overwritten.
  2. When the same key appears multiple times in .env, the first declaration takes effect.
  3. If the file is malformed, no variable is modified.

§Errors

Returns Error if the file cannot be read (missing, permissions, invalid UTF-8, etc.) or if the .env file is malformed.

§Thread safety

This function modifies the process environment. Call it before spawning any threads; see the crate-level documentation.

§Example

if let Err(e) = dotenv::load() {
    eprintln!("Failed to load .env: {e}");
}