title: What is the correct way to use lifetimes with a struct in Rust?
body: <p>I want to write this structure:</p>
  
  <pre><code>struct A {
      b: B,
      c: C,
  }
  
  struct B {
      c: &amp;C,
  }
  
  struct C;
  </code></pre>
  
  <p>The <code>B.c</code> should be borrowed from <code>A.c</code>.</p>
  
  <pre class="lang-none prettyprint-override"><code>A -&gt;
    b: B -&gt;
      c: &amp;C -- borrow from --+
                             |
    c: C  &lt;------------------+
  </code></pre>
  
  <p>This is what I tried:
      struct C;</p>
  
  <pre><code>struct B&lt;'b&gt; {
      c: &amp;'b C,
  }
  
  struct A&lt;'a&gt; {
      b: B&lt;'a&gt;,
      c: C,
  }
  
  impl&lt;'a&gt; A&lt;'a&gt; {
      fn new&lt;'b&gt;() -&gt; A&lt;'b&gt; {
          let c = C;
          A {
              c: c,
              b: B { c: &amp;c },
          }
      }
  }
  
  fn main() {}
  </code></pre>
  
  <p>But it fails:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `c` does not live long enough
    --&gt; src/main.rs:17:24
     |
  17 |             b: B { c: &amp;c },
     |                        ^ borrowed value does not live long enough
  18 |         }
  19 |     }
     |     - borrowed value only lives until here
     |
  note: borrowed value must be valid for the lifetime 'b as defined on the method body at 13:5...
    --&gt; src/main.rs:13:5
     |
  13 |     fn new&lt;'b&gt;() -&gt; A&lt;'b&gt; {
     |     ^^^^^^^^^^^^^^^^^^^^^
  
  error[E0382]: use of moved value: `c`
    --&gt; src/main.rs:17:24
     |
  16 |             c: c,
     |                - value moved here
  17 |             b: B { c: &amp;c },
     |                        ^ value used here after move
     |
     = note: move occurs because `c` has type `C`, which does not implement the `Copy` trait
  </code></pre>
  
  <p>I've read the Rust documentation on ownership, but I still don't know how to fix it.</p>