Skip to main content

parse

Function parse 

Source
pub fn parse(input: &str) -> Result<HashMap<&str, Cow<'_, str>>, ParseError>
Expand description

Parses .env content into a map of key/value pairs.

Duplicate keys follow the same rule as load: the first declaration wins. Iteration order of the returned map is unspecified. Escape sequences in double-quoted values are expanded (see the crate-level documentation).

Keys and values borrow from input. A value that contains escape sequences is materialised and returned as Cow::Owned; every other value is a Cow::Borrowed slice of input. Use Cow::as_ref (or .as_ref()) to obtain a plain &str, and convert with into_iter().map(|(k, v)| (k.to_owned(), v.into_owned())) if the map must outlive input.

This function does not touch the process environment, so it is safe to call from any thread and useful for validating or inspecting .env content.

§Errors

Returns ParseError if the content is malformed.

§Example

let vars = dotenv::parse("A=1\nB=two\nA=ignored").unwrap();
assert_eq!(vars["A"].as_ref(), "1");
assert_eq!(vars["B"].as_ref(), "two");
assert_eq!(vars.len(), 2);