title: What does "mismatched types: expected `()`" mean when using an if expression?
body: <p>I tried to implement fizzbuzz in Rust and failed with some arcane error:</p>
  
  <pre><code>fn main() {
      let mut i = 1;
  
      while i &lt; 100 {
          println!(
              "{}{}{}",
              if i % 3 == 0 { "Fizz" },
              if i % 5 == 0 { "Buzz" },
              if !(i % 3 == 0 || i % 5 == 0) { i },
          );
          i += 1;
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: mismatched types: expected `()` but found `&amp;'static str` (expected () but found &amp;-ptr)
                   if i % 3 == 0 { "Fizz" },
                                 ^~~~~~~~~~
  error: mismatched types: expected `()` but found `&amp;'static str` (expected () but found &amp;-ptr)
                   if i % 5 == 0 { "Buzz" },
                                 ^~~~~~~~~~
  error: mismatched types: expected `()` but found `&lt;generic integer #0&gt;` (expected () but found integral variable)
                   if !(i % 3 == 0 || i % 5 == 0) {
                       i
                   });
  </code></pre>
  
  <p>Newer versions of Rust have a slightly modified error message:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0317]: if may be missing an else clause
   --&gt; src/main.rs:7:13
    |
  7 |             if i % 3 == 0 { "Fizz" },
    |             ^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found &amp;str
    |
    = note: expected type `()`
               found type `&amp;str`
  
  error[E0317]: if may be missing an else clause
   --&gt; src/main.rs:8:13
    |
  8 |             if i % 5 == 0 { "Buzz" },
    |             ^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found &amp;str
    |
    = note: expected type `()`
               found type `&amp;str`
  
  error[E0317]: if may be missing an else clause
   --&gt; src/main.rs:9:13
    |
  9 |             if !(i % 3 == 0 || i % 5 == 0) { i },
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found integral variable
    |
    = note: expected type `()`
               found type `{integer}`
  </code></pre>
  
  <p>I found <a href="https://stackoverflow.com/questions/24502282/why-does-removing-return-give-me-an-error-expected-but-found">why does removing return give me an error: expected &#39;()&#39; but found</a>, but adding <code>return</code> as suggested didn't help.</p>
  
  <p>What do these errors mean and how do I avoid them in the future?</p>