title: What lifetimes do I use to create Rust structs that reference each other cyclically?
body: <p>I'd like to have struct members that know their parent. This is approximately what I'm trying to do:</p>
  
  <pre><code>struct Parent&lt;'me&gt; {
      children: Vec&lt;Child&lt;'me&gt;&gt;,
  }
  
  struct Child&lt;'me&gt; {
      parent: &amp;'me Parent&lt;'me&gt;,
      i: i32,
  }
  
  fn main() {
      let mut p = Parent { children: vec![] };
      let c1 = Child { parent: &amp;p, i: 1 };
      p.children.push(c1);
  }
  </code></pre>
  
  <p>I tried to appease the compiler with lifetimes without completely understanding what I was doing. </p>
  
  <p>Here's the error message I'm stuck on:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0502]: cannot borrow `p.children` as mutable because `p` is also borrowed as immutable
    --&gt; src/main.rs:13:5
     |
  12 |     let c1 = Child { parent: &amp;p, i: 1 };
     |                               - immutable borrow occurs here
  13 |     p.children.push(c1);
     |     ^^^^^^^^^^ mutable borrow occurs here
  14 | }
     | - immutable borrow ends here
  </code></pre>
  
  <p>That makes some sense, but I'm not at all sure where to go from here.</p>