title: When I can use either Cell or RefCell, which should I choose?
body: <p>From the <a href="http://doc.rust-lang.org/std/cell/index.html" rel="noreferrer"><code>std::cell</code> documentation</a>, I see that <code>Cell</code> is "only compatible with types that implement <code>Copy</code>". This means I must use <code>RefCell</code> for non-<code>Copy</code> types.</p>
  
  <p>When I <em>do</em> have a <code>Copy</code> type, is there a benefit to using one type of cell over another? I assume the answer is "yes", because otherwise both types wouldn't exist! What are the benefits and tradeoffs of using one type over the other?</p>
  
  <p>Here's a silly, made-up example that uses both <code>Cell</code> and <code>RefCell</code> to accomplish the same goal:</p>
  
  <pre><code>use std::cell::{Cell,RefCell};
  
  struct ThingWithCell {
      counter: Cell&lt;u8&gt;,
  }
  
  impl ThingWithCell {
      fn new() -&gt; ThingWithCell {
          ThingWithCell { counter: Cell::new(0) }
      }
  
      fn increment(&amp;self) {
          self.counter.set(self.counter.get() + 1);
      }
  
      fn count(&amp;self) -&gt; u8 { self.counter.get() }
  }
  
  struct ThingWithRefCell {
      counter: RefCell&lt;u8&gt;,
  }
  
  impl ThingWithRefCell {
      fn new() -&gt; ThingWithRefCell {
          ThingWithRefCell { counter: RefCell::new(0) }
      }
  
      fn increment(&amp;self) {
          let mut counter = self.counter.borrow_mut();
          *counter = *counter + 1;
      }
  
      fn count(&amp;self) -&gt; u8 { *self.counter.borrow_mut() }
  }
  
  
  fn main() {
      let cell = ThingWithCell::new();
      cell.increment();
      println!("{}", cell.count());
  
      let cell = ThingWithRefCell::new();
      cell.increment();
      println!("{}", cell.count());
  }
  </code></pre>