title: How to iterate over and filter an array?
body: <p>I'm trying to write a program that involves filtering and folding over arrays. I've been using <a href="https://doc.rust-lang.org/book/first-edition/iterators.html" rel="noreferrer"><em>The Rust Programming Language</em>, first edition</a> as a reference, but I don't understand what happens when I form iterators over arrays. Here is an example:</p>
  
  <pre><code>fn compiles() {
      let range = (1..6);
      let range_iter = range.into_iter();
      range_iter.filter(|&amp;x| x == 2);
  }
  
  fn does_not_compile() {
      let array = [1, 4, 3, 2, 2];
      let array_iter = array.into_iter();
      //13:34 error: the trait `core::cmp::PartialEq&lt;_&gt;` is not implemented for the type `&amp;_` [E0277]
      array_iter.filter(|&amp;x| x == 2);
  }
  
  fn janky_workaround() {
      let array = [1, 4, 3, 2, 2];
      let array_iter = array.into_iter();
      // Note the dereference in the lambda body
      array_iter.filter(|&amp;x| *x == 2);
  }
  </code></pre>
  
  <p>(<a href="https://play.rust-lang.org/?gist=8afca446c40e23f597b4&amp;version=stable" rel="noreferrer">Rust playground</a>)</p>
  
  <p>In the first function, I follow that the iterator over the range does not take ownership, so I must take a <code>&amp;x</code> in <code>filter</code>'s lambda, but I don't understand why the second example with the array behaves differently.</p>