<p dir="auto">This is part of the Rust tutorial session. The whole purpose of the exercise was to find a mistake in the block expression.</p>
<p dir="auto">See Exercise 2.1<br>
<a href="http://pnkfelix.github.io/cyot/tutorial/exercises/ex_part_2" rel="nofollow">http://pnkfelix.github.io/cyot/tutorial/exercises/ex_part_2</a></p>
<p dir="auto">With rust compile this:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="fn foo() -&gt; i32 {
   42
}

pub fn main() {

    let the_sum :i32 = {
        foo();
    };

    let the_other_sum = {
        foo();
    };

}"><pre class="notranslate"><span class="pl-k">fn</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span> -&gt; <span class="pl-smi">i32</span> <span class="pl-kos">{</span>
   <span class="pl-c1">42</span>
<span class="pl-kos">}</span>

<span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-k">let</span> the_sum <span class="pl-kos">:</span><span class="pl-smi">i32</span> = <span class="pl-kos">{</span>
        <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>

    <span class="pl-k">let</span> the_other_sum = <span class="pl-kos">{</span>
        <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">;</span>

<span class="pl-kos">}</span></pre></div>
<p dir="auto">It outputs this error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="bug_block.rs:7:24: 9:6 error: mismatched types:
 expected `i32`,
    found `()`
(expected i32,
    found ()) [E0308]
bug_block.rs:7     let the_sum :i32 = {
bug_block.rs:8         foo();
bug_block.rs:9     };
error: aborting due to previous error"><pre class="notranslate"><code class="notranslate">bug_block.rs:7:24: 9:6 error: mismatched types:
 expected `i32`,
    found `()`
(expected i32,
    found ()) [E0308]
bug_block.rs:7     let the_sum :i32 = {
bug_block.rs:8         foo();
bug_block.rs:9     };
error: aborting due to previous error
</code></pre></div>
<p dir="auto">It should also put a warning for line 12: "possibly missing return value". This would catch the bug.</p>
<p dir="auto">When <code class="notranslate">if</code> and <code class="notranslate">else</code> arms have incompatible types, like below, the compiler should point at both arms with the evaluated type and also suggest removal of trailing semicolon if relevant.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="fn main() {
    let a = if false { &quot;x&quot; } else { &quot;y&quot;; };
}"><pre class="notranslate"><code class="notranslate">fn main() {
    let a = if false { "x" } else { "y"; };
}
</code></pre></div>
<p dir="auto"><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=cd54d41c43d1b10a53aef3fd1bd0c848" rel="nofollow">Current</a>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="error[E0308]: if and else have incompatible types
 --&gt; src/main.rs:2:13
  |
2 |     let a = if false { &quot;x&quot; } else { &quot;y&quot;; };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &amp;str, found ()
  |
  = note: expected type `&amp;str`
             found type `()`"><pre class="notranslate"><code class="notranslate">error[E0308]: if and else have incompatible types
 --&gt; src/main.rs:2:13
  |
2 |     let a = if false { "x" } else { "y"; };
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &amp;str, found ()
  |
  = note: expected type `&amp;str`
             found type `()`
</code></pre></div>
<p dir="auto">Suggested:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="error[E0308]: if and else have incompatible types
 --&gt; src/main.rs:2:13
  |
2 |     let a = if false { &quot;x&quot; } else { &quot;y&quot;; };
  |                        ---          ^^^- help: remove this semicolon
  |                        |              |
  |                        |              expected &amp;str, found ()
  |                        this arm evaluates to &amp;str
  = note: expected type `&amp;str`
             found type `()`"><pre class="notranslate"><code class="notranslate">error[E0308]: if and else have incompatible types
 --&gt; src/main.rs:2:13
  |
2 |     let a = if false { "x" } else { "y"; };
  |                        ---          ^^^- help: remove this semicolon
  |                        |              |
  |                        |              expected &amp;str, found ()
  |                        this arm evaluates to &amp;str
  = note: expected type `&amp;str`
             found type `()`
</code></pre></div>
<p dir="auto"><em>Example taken from <a href="https://alantrick.ca/writings/programming/python_to_rust/" rel="nofollow">lessons learned in Converting a Python library to Rust</a> article</em></p>