title: How can I organize multiple references to the same object in Rust with reference counting?
body: <p>I have a struct representing polynome and I want multiple polynomes to reference the same <strong>mutable</strong> <code>Aliases</code> object. In JavaScript, I'd create an instance of the <code>Aliases</code> object and assign it to <code>p1.aliases</code>, <code>p2.aliases</code> and so on. In Rust, as far as I understand, I need to use <code>RefCell</code>, which should perform reference counting, because there is no garbage collector in Rust:</p>
  
  <pre><code>extern crate rand;
  
  use std::collections::HashMap;
  use std::cell::RefCell;
  use std::collections::*;
  
  pub struct Aliases {
      pub idx_to_name: HashMap&lt;i16, String&gt;,
      pub name_to_idx: HashMap&lt;String, i16&gt;,
  }
  
  pub struct Polynome {
      pub poly: BTreeMap&lt;i16, f64&gt;,
      pub aliases: RefCell&lt;Aliases&gt;,
  }
  
  impl Aliases {
      pub fn new() -&gt; Aliases {
          Aliases {
              idx_to_name: HashMap::new(),
              name_to_idx: HashMap::new(),
          }
      }
  }
  
  impl Polynome {
      pub fn new() -&gt; Polynome {
          Polynome {
              poly: BTreeMap::new(),
              aliases: RefCell::new(Aliases::new()),
          }
      }
      pub fn clone(&amp;self) -&gt; Polynome {
          Polynome {
              poly: self.poly.clone(),
              aliases: self.aliases,
          }
      }
  }
  </code></pre>
  
  <p>But I get compilation error in <code>clone()</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0507]: cannot move out of borrowed content
    --&gt; src/main.rs:36:22
     |
  36 |             aliases: self.aliases,
     |                      ^^^^ cannot move out of borrowed content
  </code></pre>
  
  <p>what is the right way to clone <code>Polynome</code>, so that all clones reference the same <code>Aliases</code> instance?</p>
  
  <p>Please note that this question is not duplicate of <a href="https://stackoverflow.com/questions/28418584/how-to-represent-shared-mutable-state">How to represent shared mutable state?</a>. Because if I try to organize reference with lifetime declaration as proposed there:</p>
  
  <pre><code>pub struct Polynome&lt;'a&gt; {
      pub poly: BTreeMap&lt;i16, f64&gt;,
      pub aliases: &amp;'a RefCell&lt;Aliases&gt; 
  }
  </code></pre>
  
  <p>then I get 'doesn't live long enough' problem instead of 'can not move out':</p>
  
  <pre><code>pub fn new() -&gt; Polynome&lt;'a&gt; {
      let alias= RefCell::new (Aliases::new());
      Polynome {
          poly: BTreeMap::new(),
          aliases: &amp;alias,
      }
  }
  
       error[E0597]: `alias` does not live long enough
     |
  24 |             aliases: &amp;alias,
     |                       ^^^^^ does not live long enough
  25 |         }
  26 |     }
     |     - borrowed value only lives until here
     |
  note: borrowed value must be valid for the lifetime 'a as defined on the impl at 19:1...
  </code></pre>
  
  <p>How should I create Aliases instance if I need it to live outside ::new()?</p>