title: Can you clone a closure?
body: <p>A <code>FnMut</code> closure cannot be cloned, for obvious reasons, but a <code>Fn</code> closure has an immutable scope; is there some way to create a "duplicate" of a <code>Fn</code> closure?</p>
  
  <p>Trying to clone it results in:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0599]: no method named `clone` found for type `std::boxed::Box&lt;std::ops::Fn(i8, i8) -&gt; i8 + std::marker::Send + 'static&gt;` in the current scope
    --&gt; src/main.rs:22:25
     |
  22 |             fp: self.fp.clone(),
     |                         ^^^^^
     |
     = note: self.fp is a function, perhaps you wish to call it
     = note: the method `clone` exists but the following trait bounds were not satisfied:
             `std::boxed::Box&lt;std::ops::Fn(i8, i8) -&gt; i8 + std::marker::Send&gt; : std::clone::Clone`
  </code></pre>
  
  <p>Is it safe to somehow pass a raw pointer to a <code>Fn</code> around, like:</p>
  
  <pre><code>let func_pnt = &amp;mut Box&lt;Fn&lt;...&gt; + Send&gt; as *mut Box&lt;Fn&lt;...&gt;&gt;
  </code></pre>
  
  <p>Technically, the above works, but it seems quite weird.</p>
  
  <p>Here's an example of what I'm trying to do:</p>
  
  <pre><code>use std::thread;
  
  struct WithCall {
      fp: Box&lt;Fn(i8, i8) -&gt; i8 + Send&gt;,
  }
  
  impl WithCall {
      pub fn new(fp: Box&lt;Fn(i8, i8) -&gt; i8 + Send&gt;) -&gt; WithCall {
          WithCall { fp: fp }
      }
  
      pub fn run(&amp;self, a: i8, b: i8) -&gt; i8 {
          (self.fp)(a, b)
      }
  }
  
  impl Clone for WithCall {
      fn clone(&amp;self) -&gt; WithCall {
          WithCall {
              fp: self.fp.clone(),
          }
      }
  }
  
  fn main() {
      let adder = WithCall::new(Box::new(|a, b| a + b));
      println!("{}", adder.run(1, 2));
  
      let add_a = adder.clone();
      let add_b = adder.clone();
  
      let a = thread::spawn(move || {
          println!("In remote thread: {}", add_a.run(10, 10));
      });
  
      let b = thread::spawn(move || {
          println!("In remote thread: {}", add_b.run(10, 10));
      });
  
      a.join().expect("Thread A panicked");
      b.join().expect("Thread B panicked");
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?gist=e79470785a64f9f432b22f23bdf6557b&amp;version=stable" rel="noreferrer">playground</a></p>
  
  <p>I have a struct with a boxed closure in it, and I need to pass that struct to a number of threads. I can't, but I also can't clone it, because you can't clone a <code>Box&lt;Fn&lt;&gt;&gt;</code> and you can't clone a <code>&amp;Fn&lt;...&gt;</code>.</p>