Evaluating to Values
I mentioned that an if
is an expression. And if you remember from Anatomy of Rust:
An expression is evaluated to a value of a specific type
So far, every if
expression we’ve had evaluates to unit. However, we can have it evaluate to something else, with the following restriction:
-
There must be an
else
at the end -
Each block for each
if
,else
, andelse if
in the expression must evaluate to the same type
For example, this code is good: