title: How to move the lifetime of references outside a scope in Rust
body: <p>Actually I try to implement the following functionality in Rust.</p>
  <p>I want to have a structure Node which has a vector to some other Node structures. In addition I have a master vector which keeps all the Node structures which have been instantiated.</p>
  <p>The key point here is that the Nodes are allocated within a loop (i.e. an own scope) and the master vector keeping all the structures (or references to the structures) is declared outside the loop which is in my opinion a 0815 use case.</p>
  <p>After a lot of trying I came up with this code which still does not compile. Actually I tried it with just &amp;Node and alternatively with RefCell&lt;&amp;Node&gt;, both do not compile.</p>
  <pre><code>struct Node&lt;'a&gt; {
      name: String,
      nodes: RefCell&lt;Vec&lt;&amp;'a Node&lt;'a&gt;&gt;&gt;,
  }
  
  impl&lt;'a&gt; Node&lt;'a&gt; {
  
      fn create(name: String) -&gt; Node&lt;'a&gt; {
          Node {
              name: name,
              nodes: RefCell::new(Vec::new()),
          }
      }
      
      fn add(&amp;self, value: &amp;'a Node&lt;'a&gt;) {
          self.nodes.borrow_mut().push(value);
      }
  
      fn get_nodes(&amp;self) -&gt; Vec&lt;&amp;'a Node&gt; {
          self.nodes.take()
      }
  }
  
  
  // Later the code ...
  
      let mut the_nodes_ref: HashMap&lt;String, RefCell&lt;&amp;Node&gt;&gt; = HashMap::new();
      let mut the_nodes_nodes: HashMap&lt;String, &amp;Node&gt; = HashMap::new();
  
      // This works
      let no1_out = Node::create(String::from(&quot;no1&quot;));
      let no2_out = Node::create(String::from(&quot;no2&quot;));
  
      no1_out.add(&amp;no2_out);
      no2_out.add(&amp;no1_out);
  
      the_nodes_nodes.insert(no1_out.name.clone(), &amp;no1_out);
      the_nodes_nodes.insert(no2_out.name.clone(), &amp;no2_out);
  
      let no1_ref_out = RefCell::new(&amp;no1_out);
      let no2_ref_out = RefCell::new(&amp;no2_out);
  
      the_nodes_ref.insert(no1_out.name.clone(), no1_ref_out);
      the_nodes_ref.insert(no2_out.name.clone(), no2_ref_out);
  
      // This works not because no1 and no2 do not live long enough
      let items = [1, 2, 3];
      for _ in items {
          let no1 = Node::create(String::from(&quot;no1&quot;));
          let no2 = Node::create(String::from(&quot;no2&quot;));
  
          no1.add(&amp;no2); // &lt;- Error no2 lives not long enough
          no2.add(&amp;no1); // &lt;- Error no1 lives not long enough
  
          the_nodes_nodes.insert(no1.name.clone(), &amp;no1);
          the_nodes_nodes.insert(no2.name.clone(), &amp;no2);
  
          let no1_ref = RefCell::new(&amp;no1);
          let no2_ref = RefCell::new(&amp;no2);
  
          the_nodes_ref.insert(no1.name.clone(), no1_ref);
          the_nodes_ref.insert(no2.name.clone(), no2_ref);
      }
  </code></pre>
  <p>I kind of understand the problem, but I am wondering how to solve this problem. How can I allocate a structure within an separate scope (here the for loop) and then use the allocated structures outside the for loop. I mean it is a common use case to allocate a structure within a loop and use it later outside of the loop.</p>
  <p>Somehow I have the feeling that the missing link is to tell the Rust Compiler via the lifetime parameters, that the references should also stay alive outside the for loop but I have no idea how to do that. But maybe this is also not the correct way to do it ....</p>
  <p>Actually another key point here is that I want that the Nodes have references to the other Nodes and not copies of the Nodes. The same is true for the master vector, this vector should have references to the allocated Nodes and not copies of the Nodes.</p>