title: Initializing a Rust variable passed to async code such as tokio and hyper
body: <p>I have a value that cannot be computed at compile time. It needs to be computed before any of the app code runs, and then it will only be read throughout the lifetime of the app. It also needs to be passed around to executors such as <code>tokio</code> and <code>hyper</code> handlers.</p>
  
  <p>How can I create such a value safely, idiomatically and without unneeded performance losses?</p>
  
  <ul>
  <li>If I create it in <code>main</code> and pass it to <code>hyper</code>, it does not live long enough.</li>
  <li>If I create it with <code>lazy_static!</code>, it only gets computed when it's first accessed.  If it can't be computed, then I don't want to run the rest of the application either. I'd rather know I can't connect to the database when I start the application, not when a client makes a request.</li>
  <li>If I make it a <code>static mut</code>, then I can't use it in safe code.</li>
  </ul>
  
  <p>Ideally, I'd like to do something like:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[tokio::main]
  pub async fn main() {
      let db = init_db();
  
      // This uses a hyper server, passes db around
      // to tokio and hyper handlers, etc.
      run_app(&amp;db);
  }
  </code></pre>