pub struct Env { /* private fields */ }Expand description
A snapshot of environment variables.
Env stores an ordered list of (key, value) pairs. Duplicate keys are
allowed; lookups return the last matching value. This makes it useful
both for capturing the process environment and for constructing
deterministic terminal environments in tests.
Implementations§
Source§impl Env
impl Env
Sourcepub fn from_process() -> Self
pub fn from_process() -> Self
Capture the current process environment.
§Returns
An Env containing all variables yielded by std::env::vars at the
time of the call.
§Errors and panics
This method does not return errors. It has the same panic behavior as
std::env::vars if the process environment contains invalid data.
Sourcepub fn from_pairs<I, K, V>(iter: I) -> Self
pub fn from_pairs<I, K, V>(iter: I) -> Self
Build an environment from (key, value) pairs.
Pair order and duplicate keys are preserved. Later duplicates shadow
earlier values for get, has, and
is_truthy.
§Parameters
iter— variables to store.
§Returns
An Env containing the supplied variables.
§Errors and panics
This method does not return errors. It may panic only if allocation for the stored strings fails.
Sourcepub fn set(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
pub fn set( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self
Append a variable to the snapshot.
If key already exists, the new value shadows earlier values for
get, is_truthy, and has
lookups. Existing entries are not removed.
§Parameters
key— variable name.value— variable value.
§Returns
self, for chaining.
§Errors and panics
This method does not return errors. It may panic only if allocation for the stored strings fails.
Sourcepub fn get(&self, key: &str) -> Option<String>
pub fn get(&self, key: &str) -> Option<String>
Return a variable’s value.
If duplicate keys are present, the last value is returned.
§Parameters
key— variable name.
§Returns
A newly allocated copy of the value, or None if key is absent.
§Errors and panics
This method does not return errors. It may panic only if allocation for the returned string fails.
Sourcepub fn is_truthy(&self, key: &str) -> bool
pub fn is_truthy(&self, key: &str) -> bool
Return whether a variable parses as a truthy boolean.
Accepts 1, t, T, TRUE, true, and True. Anything else,
including an empty or absent value, is false.
§Parameters
key— variable name.
§Returns
true for the accepted truthy values.
§Errors and panics
This method does not fail or intentionally panic.