env in std - Rust (original) (raw)

macro_rules! env {
    ($name:expr $(,)?) => { ... };
    ($name:expr, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>e</mi><mi>r</mi><mi>r</mi><mi>o</mi><msub><mi>r</mi><mi>m</mi></msub><mi>s</mi><mi>g</mi><mo>:</mo><mi>e</mi><mi>x</mi><mi>p</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">error_msg:expr </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">erro</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">m</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span></span></span></span>(,)?) => { ... };
}

Expand description

Inspects an environment variable at compile time.

This macro will expand to the value of the named environment variable at compile time, yielding an expression of type &'static str. Usestd::env::var instead if you want to read the value at runtime.

If the environment variable is not defined, then a compilation error will be emitted. To not emit a compile error, use the option_env!macro instead. A compilation error will also be emitted if the environment variable is not a valid Unicode string.

§Examples

let path: &'static str = env!("PATH");
println!("the $PATH variable at the time of compiling was: {path}");

You can customize the error message by passing a string as the second parameter:

let doc: &'static str = env!("documentation", "what's that?!");

If the documentation environment variable is not defined, you’ll get the following error: