Skip to main content

Env

Trait Env 

Source
pub trait Env: Send + Sync {
    // Required method
    fn get(&self, key: &str) -> Option<String>;

    // Provided method
    fn has(&self, key: &str) -> bool { ... }
}
Expand description

A read-only view of environment variables.

Implementations decide where the variables come from and whether they can change: ProcessEnv reads the live process environment on every lookup, while EnvList answers from a fixed list.

Only get needs implementing; has is derived from it.

Required Methods§

Source

fn get(&self, key: &str) -> Option<String>

Return a variable’s value.

§Parameters
  • key — variable name.
§Returns

The value, or None if key is absent.

§Errors and panics

Implementations should not panic for an absent or malformed variable.

Provided Methods§

Source

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

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

Overriding this is only for taking a shortcut, not for changing the answer: it must stay equivalent to get(key).is_some_and(|v| !v.is_empty()).

§Parameters
  • key — variable name.
§Returns

true if key is present and its value is not empty.

§Errors and panics

This method does not fail or intentionally panic.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: Env + ?Sized> Env for Box<T>

Source§

fn get(&self, key: &str) -> Option<String>

Source§

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

Implementors§