#default-value #env #macro #option-env

macro default-env

The env! macro but with a default value

2 releases

0.1.1 Jun 21, 2019
0.1.0 Jun 21, 2019

#1662 in Rust patterns

Download history 1559/week @ 2023-12-10 1600/week @ 2023-12-17 2871/week @ 2023-12-24 3000/week @ 2023-12-31 2242/week @ 2024-01-07 2409/week @ 2024-01-14 2129/week @ 2024-01-21 1968/week @ 2024-01-28 2556/week @ 2024-02-04 3248/week @ 2024-02-11 2856/week @ 2024-02-18 2991/week @ 2024-02-25 3234/week @ 2024-03-03 3284/week @ 2024-03-10 4489/week @ 2024-03-17 3114/week @ 2024-03-24

14,477 downloads per month
Used in 10 crates (4 directly)

MIT license

3KB

default-env

default_env! is a macro like env! that returns a default value if the environment variable is not found. Unlike option_env!, the output of default_env! can be used in macros (because who doesn't love macros in their macros?).

Example

macro_rules! long_str {
  () => {
    concat!(
        "Hello, ", default_env!("USER", "anonymous user"), ".",
        "Today is ", default_env!("WEEKDAY", compile_error!("You exist in a land beyond time."))
      )
  }
}

Dependencies

~2MB
~45K SLoC