title: Passing a closure that modifies its environment to a function in Rust
body: <p>I have a closure that captures and modifies its environment. I want to pass this closure to a function that accepts closures:</p>
  
  <pre><code>fn main() {
      let mut integer = 5;
      let mut closure_variable = || -&gt; i32 {
          integer += 1;
          integer
      };
      execute_closure(&amp;mut closure_variable);
  }
  
  fn execute_closure(closure_argument: &amp;mut Fn() -&gt; i32) {
      let result = closure_argument();
      println!("Result of closure: {}", result);
  }
  </code></pre>
  
  <p>Because the closure modifies its environment, this fails:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
   --&gt; src/main.rs:3:32
    |
  3 |       let mut closure_variable = || -&gt; i32 {
    |  ________________________________^
  4 | |         integer += 1;
  5 | |         integer
  6 | |     };
    | |_____^
  7 |       execute_closure(&amp;mut closure_variable);
    |                       --------------------- the requirement to implement `Fn` derives from here
    |
  note: closure is `FnMut` because it mutates the variable `integer` here
   --&gt; src/main.rs:4:9
    |
  4 |         integer += 1;
    |         ^^^^^^^
  </code></pre>
  
  <p>As I understand from <a href="https://stackoverflow.com/questions/30177395">When does a closure implement Fn, FnMut and FnOnce?</a>, this means that my closure actually is expanded to a struct that implements the trait <code>FnMut</code>. This trait is mutable, meaning calling the function changes the (implicit) object. I think this correct, because the variable <code>integer</code> should be modified after calling <code>execute_closure()</code>.</p>
  
  <p>How do I convince the compiler this is okay and that I actually want to call a <code>FnMut</code> function? Or is there something fundamentally wrong with how I use Rust in this example?</p>