title: How to write a Rust function that takes an iterator?
body: <p>I'd like to write a function that accepts an iterator and returns the results of some operations on it.  Specifically, I'm trying to iterate over the values of a <code>HashMap</code>:</p>
  
  <pre><code>use std::collections::HashMap;
  
  fn find_min&lt;'a&gt;(vals: Iterator&lt;Item=&amp;'a u32&gt;) -&gt; Option&lt;&amp;'a u32&gt; {
      vals.min()
  }
  
  fn main() {
      let mut map = HashMap::new();
      map.insert("zero", 0u32);
      map.insert("one", 1u32);
      println!("Min value {:?}", find_min(map.values()));
  }
  </code></pre>
  
  <p>But alas:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: the `min` method cannot be invoked on a trait object
   --&gt; src/main.rs:4:10
    |
  4 |     vals.min()
    |          ^^^
  
  error[E0277]: the trait bound `std::iter::Iterator&lt;Item=&amp;'a u32&gt; + 'static: std::marker::Sized` is not satisfied
   --&gt; src/main.rs:3:17
    |
  3 | fn find_min&lt;'a&gt;(vals: Iterator&lt;Item = &amp;'a u32&gt;) -&gt; Option&lt;&amp;'a u32&gt; {
    |                 ^^^^ `std::iter::Iterator&lt;Item=&amp;'a u32&gt; + 'static` does not have a constant size known at compile-time
    |
    = help: the trait `std::marker::Sized` is not implemented for `std::iter::Iterator&lt;Item=&amp;'a u32&gt; + 'static`
    = note: all local variables must have a statically known size
  
  error[E0308]: mismatched types
    --&gt; src/main.rs:11:41
     |
  11 |     println!("Min value {:?}", find_min(map.values()));
     |                                         ^^^^^^^^^^^^ expected trait std::iter::Iterator, found struct `std::collections::hash_map::Values`
     |
     = note: expected type `std::iter::Iterator&lt;Item=&amp;u32&gt; + 'static`
                found type `std::collections::hash_map::Values&lt;'_, &amp;str, u32&gt;`
  </code></pre>
  
  <p>I get the same error if I try to pass by reference; if I use a <code>Box</code>, I get lifetime errors.</p>