title: How do I convert the "largest value in a Vec" example in the Rust book to not use the Copy trait?
body: <p>I'm trying to accomplish an exercise "left to the reader" in <a href="https://doc.rust-lang.org/book/2018-edition/ch10-02-traits.html" rel="nofollow noreferrer">the 2018 Rust book</a>. The example they have, 10-15, uses the <code>Copy</code> trait. However, they recommend implementing the same without <code>Copy</code> and I've been really struggling with it.</p>
  
  <p>Without <code>Copy</code>, I cannot use <code>largest = list[0]</code>. The compiler recommends using a reference instead. I do so, making <code>largest</code> into a <code>&amp;T</code>. The compiler then complains that the <code>largest</code> used in the comparison is a <code>&amp;T</code>, not <code>T</code>, so I change it to <code>*largest</code> to dereference the pointer. This goes fine, but then stumbles on <code>largest = item</code>, with complaints about <code>T</code> instead of <code>&amp;T</code>. I switch to <code>largest = &amp;item</code>. Then I get an error I cannot deal with:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `item` does not live long enough
   --&gt; src/main.rs:6:24
    |
  6 |             largest = &amp;item;
    |                        ^^^^ borrowed value does not live long enough
  7 |         }
  8 |     }
    |     - borrowed value only lives until here
    |
  note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 1:1...
  </code></pre>
  
  <p>I do not understand how to lengthen the life of this value. It lives and dies in the <code>list.iter()</code>. How can I extend it while still only using references?</p>
  
  <p>Here is my code for reference:</p>
  
  <pre><code>fn largest&lt;T: PartialOrd&gt;(list: &amp;[T]) -&gt; &amp;T {
      let mut largest = &amp;list[0];
  
      for &amp;item in list.iter() {
          if item &gt; *largest {
              largest = &amp;item;
          }
      }
  
      largest
  }
  </code></pre>