Skip to main content

Env

Struct Env 

Source
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

Source

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.

Source

pub fn new() -> Self

Build an empty environment.

§Returns

An Env with no variables.

§Errors and panics

This method does not fail or intentionally panic.

Source

pub fn from_pairs<I, K, V>(iter: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

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.

Source

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.

Source

pub fn has(&self, key: &str) -> bool

Return whether a variable is present with a non-empty value.

§Parameters
  • key — variable name.
§Returns

true if the last value for key exists and is not empty.

§Errors and panics

This method does not fail or intentionally panic.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Env

Source§

fn clone(&self) -> Env

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Env

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Env

Source§

fn default() -> Env

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Env

§

impl RefUnwindSafe for Env

§

impl Send for Env

§

impl Sync for Env

§

impl Unpin for Env

§

impl UnsafeUnpin for Env

§

impl UnwindSafe for Env

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,