title: inconsistent non-exhaustive pattern matching with if statements
body: <p>I've wrote two equivalent functions in Rust, one compiles the other doesn't.</p>
  <p>Program 1:</p>
  <pre class="lang-rust prettyprint-override"><code>pub fn majority_element&lt;T: Eq&gt;(nums: &amp;[T]) -&gt; Option&lt;&amp;T&gt; {
      nums.iter().fold(None, |acc, curr| {
          match acc {
              None =&gt; Some((1, curr)),
              Some((0, prev)) if prev != curr =&gt; Some((1, curr)),
              Some((count, prev)) =&gt; Some((count + if prev == curr { 1 } else { -1 }, prev)),
          }
      }).map(|(_, val)| val)
  }
  </code></pre>
  <p>program 2 (doesn't compile)</p>
  <pre class="lang-rust prettyprint-override"><code>pub fn majority_element&lt;T: Eq&gt;(nums: &amp;[T]) -&gt; Option&lt;&amp;T&gt; {
      nums.iter().fold(None, |acc, curr| {
          match acc {
              None =&gt; Some((1, curr)),
              Some((0, prev)) if prev != curr =&gt; Some((1, curr)),
              Some((count, prev)) if prev == curr =&gt; Some((count + 1, prev)),
              Some((count, prev)) if prev != curr =&gt; Some((count - 1, prev)),
          }
      }).map(|(_, val)| val)
  }
  </code></pre>
  <pre><code>error[E0004]: non-exhaustive patterns: `Some(_)` not covered
   --&gt; src/lib.rs:3:15
    |
  3 |         match acc {
    |               ^^^ pattern `Some(_)` not covered
    |
  </code></pre>
  <p>I prefer the second one because it expresses more clearly my intent.</p>
  <p>Is this a compiler quirk or my pattern is actually not exhaustive? (and if so what am I missing?)<br />
  The type of acc is: <code>Option&lt;(usize, &amp;T)&gt;</code></p>