title: What does the rust compiler "error: can't capture dynamic environment in a fn item; use the || { ... } closure form instead" mean, and how to fix it?
body: <p>I get a rust compiler error:</p>
  
  <pre><code>src/main.rs:33:31: 33:35 error: can't capture dynamic environment in a fn item; use the || { ... } closure form instead
  </code></pre>
  
  <p>The error occurs because I have a function in which I declare a variable using <code>let</code>, and then have an inner function in which I try to use this variable that is closed over.</p>
  
  <p>I am fairly certain that this is a beginner question, so sorry in advance if this has a very straight forward answer!</p>
  
  <p>Note that I am using this inner function as a callback somewhere, and thus using a closure, like</p>
  
  <pre><code>let closure_fn = | args | -&gt; () { do stuff };
  </code></pre>
  
  <p>... is not going to be an appropriate solution for me.</p>
  
  <hr>
  
  <pre><code>extern crate nickel;
  
  use std::io::net::ip::Ipv4Addr;
  use nickel::{ Nickel, Request, Response };
  
  fn stub_3rd_party_function() -&gt; String {
      "hello world".to_string()
  }
  
  fn main() {
  
      let mut server = Nickel::new();
  
      // assume that the variable **must** be instantiated like this
      let hello_text : String = stub_3rd_party_function();
  
      fn hello_handler (_request: &amp;Request, response: &amp;mut Response) -&gt; () {
          response.send(hello_text.as_slice());
      }
      server.get("/hello", hello_handler);
  
      server.listen(Ipv4Addr(0,0,0,0), 6767);
  }
  </code></pre>
  
  <p>Results in the following error:</p>
  
  <pre><code>src/test.rs:12:23: 12:33 error: can't capture dynamic environment in a fn item; use the || { ... } closure form instead
  src/test.rs:12         response.send(hello_text);
                                       ^~~~~~~~~~
  src/test.rs:12:23: 12:33 error: unresolved name `hello_text`.
  src/test.rs:12         response.send(hello_text);
                                       ^~~~~~~~~~
  error: aborting due to 2 previous errors
  </code></pre>
  
  <p>Now, I switch from a standard function to a closure function instead:</p>
  
  <pre><code>extern crate nickel;
  
  use std::io::net::ip::Ipv4Addr;
  use nickel::{ Nickel, Request, Response };
  
  fn stub_3rd_party_function() -&gt; String {
      "hello world".to_string()
  }
  
  fn main() {
  
      let mut server = Nickel::new();
  
      // assume that the variable **must** be instantiated like this
      let hello_text : String = stub_3rd_party_function();
  
      let hello_handler = |_request: &amp;Request, response: &amp;mut Response| -&gt; () {
          response.send(hello_text.as_slice());
      };
      server.get("/hello", hello_handler);
  
      server.listen(Ipv4Addr(0,0,0,0), 6767);
  }
  </code></pre>
  
  <p>Results in a different error:</p>
  
  <pre><code>src/test.rs:21:30: 21:43 error: mismatched types: expected `fn(&amp;nickel::request::Request&lt;'_&gt;, &amp;mut nickel::response::Response&lt;'_,'_&gt;)` but found `|&amp;nickel::request::Request&lt;'_&gt;, &amp;mut nickel::response::Response&lt;'_,'_&gt;|` (expected extern fn but found fn)
  src/test.rs:21         server.get("/hello", hello_handler);
                                              ^~~~~~~~~~~~~
  error: aborting due to previous error
  </code></pre>
  
  <p>Is there perhaps a way to "wrap" the closed over function with a normal one?</p>
  
  <p>Since the library that I am using expects a standard function instead of a closure,
  I cannot use a closure.
  But if I do not use a closure, I cannot close over variables that are defined within the outer function, <code>fn main () { ... }</code>... and thus getting stuck here.</p>
  
  <p>Note that above, I am using a string, <code>hello_text</code>, for the purposes of providing a concise code example.
  In this case using a static variable would suffice. However, static variables will not fix it for me, as I need to be able to assign a variable from within a <code>fn main()</code> the result of a function call, and then use that within my inner handler function.</p>