title: How to pass a variable to actix-web guard() in Rust?
body: <pre><code>#[actix_rt::main]
  async fn main() -&gt; std::io::Result&lt;()&gt; {
  
      let token = env::var(&quot;TOKEN&quot;).expect(&quot;Set TOKEN&quot;);
  
      HttpServer::new(|| {
          App::new()
              .wrap(middleware::Logger::default())
              .service(
                  web::resource(&quot;/&quot;)
                      .guard(guard::Header(&quot;TOKEN&quot;, &amp;token))
                      .route(web::post().to(index))
              )
      })
          .bind(&quot;127.0.0.1:8080&quot;)?
          .run()
          .await
  }
  </code></pre>
  <p>The error is:</p>
  <pre><code>error[E0597]: `token` does not live long enough
  </code></pre>
  <p>I saw <code>.data()</code> in actix docs but that is for passing variables inside routes functions.</p>
  <p><strong>UPD:</strong></p>
  <p>If I add &quot;move&quot;:</p>
  <pre><code>HttpServer::new(move || {
  </code></pre>
  <p>then just error changes:</p>
  <pre><code>error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
    --&gt; src/main.rs:50:58
     |
  50 |                     .guard(guard::Header(&quot;TOKEN&quot;, &amp;token))
     |                                                    ^^^^^^
     |
  note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 42:21...
    --&gt; src/main.rs:42:21
     |
  42 |     HttpServer::new(move || {
     |                     ^^^^^^^
  note: ...so that closure can access `token`
    --&gt; src/main.rs:50:58
     |
  50 |                     .guard(guard::Header(&quot;TOKEN&quot;, &amp;token))
     |                                                    ^^^^^^
     = note: but, the lifetime must be valid for the static lifetime...
  note: ...so that reference does not outlive borrowed content
    --&gt; src/main.rs:50:58
     |
  50 |                     .guard(guard::Header(&quot;TOKEN&quot;, &amp;token))
     |                                                    ^^^^^^
  
  error: aborting due to previous error
  </code></pre>