Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dotenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,24 @@ pub fn dotenv_iter() -> Result<iter::Iter<File>> {
let (_, iter) = Finder::new().find()?;
Ok(iter)
}


/// Like `dotenv`, but returns an HashMap over variables instead of loading into environment.
/// Unlike 'dotenv().ok()', this function only retrieves variables from the .env file, without loading them into the environment.
/// # Examples
/// ```no_run
/// use dotenv;
///
/// let dotenv_values: std::collections::HashMap<String, String> = dotenv::dotenv_values().unwrap();
/// dotenv_values.iter()
/// .for_each(|item| println!("{:?}", item));
///
/// ```
pub fn dotenv_values() -> Result<std::collections::HashMap<String, String>> {
let vars = dotenv_iter()?
.filter_map(|item| item.ok())
.collect::<std::collections::HashMap<String, String>>();

Ok(vars)
}