title: Cannot borrow captured outer variable in an Fn closure when using a closure with an Rc
body: <pre><code>use std::rc::Rc;
  
  fn f1(cb: Box&lt;Fn(i32) -&gt; i32&gt;) {
      let res = cb(15);
      println!("res {}", res);
  }
  
  fn main() {
      let mut v2 = Rc::new(5_i32);
  
      // 1
      // f1(Box::new(move |x: i32| *v2 + x));
  
      // 2
      f1(Box::new(move |x: i32| {
          let tmp = *v2;
          *Rc::get_mut(&amp;mut v2).unwrap() = tmp + 1;
          x + *v2
      }));
  }
  </code></pre>
  
  <p>The code referenced as "1", if uncommented, compiles and runs just fine, but the code referenced as "2" does not compile, failing with the message:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0596]: cannot borrow `v2` as mutable, as it is a captured variable in a `Fn` closure
  </code></pre>
  
  <p>How can I fix this, if I want keep the code structure as it is?</p>
  
  <p>In my real code, I want connect two traits. One of them will call a callback on event, and the other has a function to handle the callback:</p>
  
  <pre><code>trait Foo {
      fn add_callback(&amp;mut self, cb: Box&lt;Fn(i32)&gt;);
  }
  
  trait Boo {
      fn on_new_data(&amp;mut self, data: i32);
  }
  </code></pre>
  
  <p>I want to create a trait object with <code>Boo</code>, wrap it with <code>Rc</code>, and pass it to <code>Foo::add_callback</code> in the form of <code>|x:i32| Rc::get_mut(&amp;mut boo).unwrap().on_new_data(x)</code></p>