A match
expression is made up of arms. An arm consists of a pattern to match against, and the code that should be run if the value given to match
fits that armโs pattern.
This seems very similar to a conditional expression used with if, but thereโs a big difference: with if, the condition needs to evaluate to a Boolean value, but here it can be any type.
The code associated with each arm is an expression, and the resultant value of the expression in the matching arm is the value that gets returned for the entire match
expression.
We donโt typically use curly brackets if the match arm code is short. If you want to run multiple lines of code in a match arm, you must use curly brackets, and the comma following the arm is then optional.
match
is exhaustive: the armsโ patterns must cover all possibilities.
You can use catch-all patterns like this when you need the match value:
Or placeholder _
like this when you want to ignore the value but want to do something:
Or when we donโt want to do anything with unit value ()
: