title: Populating a static/const with an environment variable at runtime in Rust
body: <p>I'm trying to load passwords and sensitive data from the system's environment when my service starts up. I've tried a number of different ways but can't seem to figure out the right way to do this in Rust.</p>
  
  <pre><code>const PASSWORD: String = var("PASSWORD").unwrap();
  </code></pre>
  
  <p>Doesn't work because <code>method calls in constants are limited to constant  inherent methods</code>. The same applies to <code>static</code> (except the error says statics, obviously).</p>
  
  <p>The other way I've seen to do it is</p>
  
  <pre><code>const PASSWORD: &amp;'static str = env!("PASSWORD");
  </code></pre>
  
  <p>But the problem with that is it will be defined at compile time as <code>env!</code> is a macro (at least that is my understanding).</p>
  
  <p>I also considered simply wrapping the call to <code>var("...").unwrap()</code> in a function but that solution doesn't really sit right with me, and would also allow the values to change throughout runtime AND not validate them when the service starts.</p>
  
  <p>As you can tell I'm new to Rust. I'd really appreciate if in your answer you could not just explain how to load the const/static at runtime but also explain why what I'm doing is dumb and wrong :)</p>