The following Rust code contains a
bigger function that takes two numbers as arguments and returns a number. It also contains what might look like an if statement that guards one return value, otherwise returning b. However it doesn't compile like we might expect in other languages, even with Rust's "implicit returns".
The error we see is
expected () found i32.
The reason for this is core to Rust. Rust doesn't have "implicit returns" because (almost) everything in Rust is an expression which has a return value. The exception to this are statements of which there are only a few. One statement is
;, which takes an expression, throws away the expression's value, and evaluate to
() instead. So let's use a semicolon on our
This yields additional information in our error that points vaguely at the underlying issue:
if may be missing an else clause.
The reason for this is
if is an expression, and thus has a return value. because
if has a return value, the type of the return value from both branches has to match. By not writing an
else branch, we've declared the return value from the non-existent
else branch to be
(), which doesn't match with the return type of the
if branch, which is
i32 because that is the type of
We can prove this by moving
b into the
else branch of our
if expression and printing the return value out. (note that we've used 0 as a temporary
i32 return value for our function while we print to keep everything running).
which given a call such as
bigger(42, 20) would print out the following.
So in the end because
if expressions are indeed expressions we have to pull
b into our expression to take advantage of the return value from the
if expression as the return value of our
Finally note that you can explicitly return, which acts as you might expect in other languages.
This post was co-authored by Prince Wilson