diff --git a/dotenv/src/lib.rs b/dotenv/src/lib.rs index f2b1b0a..6d58314 100644 --- a/dotenv/src/lib.rs +++ b/dotenv/src/lib.rs @@ -179,3 +179,24 @@ pub fn dotenv_iter() -> Result> { 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 = dotenv::dotenv_values().unwrap(); +/// dotenv_values.iter() +/// .for_each(|item| println!("{:?}", item)); +/// +/// ``` +pub fn dotenv_values() -> Result> { + let vars = dotenv_iter()? + .filter_map(|item| item.ok()) + .collect::>(); + + Ok(vars) +} +