title: Copying a struct for use on another thread
body: <p>I have a struct:</p>
  
  <pre><code>struct MyData {
      x: i32
  }
  </code></pre>
  
  <p>I want to asynchronously start a long operation on this struct.</p>
  
  <p>My first attempt was this:</p>
  
  <pre><code>fn foo(&amp;self) { //should return immediately
      std::thread::Thread::spawn(move || { 
          println!("{:?}",self.x); //consider a very long operation
      });
  }
  </code></pre>
  
  <p>Clearly the compiler <code>cannot infer an appropriate lifetime due to conflicting requirements</code> because <code>self</code> may be on the stack frame and thus cannot be guaranteed to exist by the time the operation is running on a different stack frame.</p>
  
  <p>To solve this, I attempted to make a copy of <code>self</code> and provide that copy to the new thread:</p>
  
  <pre><code>fn foo(&amp;self) { //should return immediately
      let clone = self.clone();
      std::thread::Thread::spawn(move || { 
          println!("{:?}",clone.x); //consider a very long operation
      });
  }
  </code></pre>
  
  <p>I think that does not compile because now <code>clone</code> is on the stack frame which is similar to before.  I also tried to do the <code>clone</code> inside the thread, and that does not compile either, I think for similar reasons.</p>
  
  <p>Then I decided maybe I could use a <code>channel</code> to push the copied data into the thread, on the theory that perhaps <code>channel</code> can magically move (copy?) stack-allocated data between threads, which is suggested by <a href="http://doc.rust-lang.org/std/sync/mpsc/fn.channel.html#example" rel="nofollow">this example in the documentation</a>.  However the compiler cannot infer a lifetime for this either:</p>
  
  <pre><code>fn foo(&amp;self) { //should return immediately
      let (tx, rx) = std::sync::mpsc::channel();
      tx.send(self.clone());
      std::thread::Thread::spawn(move || { 
          println!("{:?}",rx.recv().unwrap().x); //consider a very long operation
      });
  }
  </code></pre>
  
  <p>Finally, I decided to just copy my struct onto the heap explicitly, and pass an Arc into the thread.  But not even here can the compiler figure out a lifetime:</p>
  
  <pre><code>fn foo(&amp;self) { //should return immediately
      let arc = std::sync::Arc::new(self.clone());
      std::thread::Thread::spawn(move || { 
          println!("{:?}",arc.clone().x); //consider a very long operation
      });
  }
  </code></pre>
  
  <p>Okay borrow checker, I give up.  How do I get a copy of <code>self</code> onto my new thread?</p>