if expressions are pretty straightforward and similar to most other languages.
Unlike other C-like languages,
there are no parentheses surrounding the condition and the braces are always required.
if can be used as an expression, last expression is the value of a block:
If you're using if as an expression, for example, for returning its value or assigning it to a variable, the else branch is mandatory.
Therefore, there is no ternary operator (
condition ? then : else) because ordinary
if works fine
in this role.
To handle Result/Optional types, there is a special
See Error handling for more information.
match statement is a shorter way to write a sequence of
When a matching branch is found, the following statement block will be run.
else branch will be run when no other branches match.
match statement can also to be used as an
if-else if-else alternative:
or as an
match expression returns the value of the final expression from the matching branch.
match statement can also be used to branch on the variants of an
by using the shorthand
else branch is not allowed when all the branches are exhaustive.
You can also use ranges as
If the value falls within the range of a branch, that branch will be executed.
Note that the ranges use
... (three dots) rather than
.. (two dots).
This is because the range is inclusive of the last element, rather than exclusive
.. ranges are).
.. in a
match branch will throw an error.
Constants can also be used in the range branch expressions.
matchas an expression is not usable in