title: Rust calling FnMut within implemented Struct
body: <p>i have the following code snippet which implements some kind of Emitter Struct:</p>
  <pre class="lang-rust prettyprint-override"><code>type Callback&lt;'a&gt; = Option&lt;&amp;'a mut dyn FnMut()&gt;;
  
  struct Emitter&lt;'a&gt; {
    cb: Callback&lt;'a&gt;
  }
  
  impl&lt;'a&gt; Emitter&lt;'a&gt; {
  
    fn emit(&amp;mut self) {
      if self.cb.is_some() {
        let f = self.cb.unwrap();
        f()
      }
    }
  }
  
  fn main() {
  
    let mut cb = || println!(&quot;test&quot;);
  
    let mut e = Emitter { 
      cb : Some(&amp;mut cb)
    };
    
    e.emit();
  }
  </code></pre>
  <p>The emit() function tries to run the saved callback clojure. But i cannot wrap my head around how to run the callback, since the code produces the following error:</p>
  <pre><code>  --&gt; src/main.rs:11:15
     |
  11 |       let f = self.cb.unwrap();
     |               ^^^^^^^
     |               |
     |               move occurs because `self.cb` has type `Option&lt;&amp;mut dyn FnMut()&gt;`, which does not implement the `Copy` trait
     |               help: consider borrowing the `Option`'s content: `self.cb.as_ref()`
  </code></pre>
  <p>Appreciate some help :)</p>
  <p>Here is the snippet on replit: <a href="https://replit.com/@lutzer/RustEmitterTest" rel="nofollow noreferrer">https://replit.com/@lutzer/RustEmitterTest</a></p>