match
Expressions
match
can be used to check a value against one or more options:
fn main() { let val = 1; match val { 1 => println!("one"), 10 => println!("ten"), 100 => println!("one hundred"), _ => { println!("something else"); } } }
Like if
expressions, match
can also return a value;
fn main() { let flag = true; let val = match flag { true => 1, false => 0, }; println!("The value of {flag} is {val}"); }
-
match
arms are evaluated from top to bottom, and the first one that matches has its corresponding body executed. -
There is no fall-through between cases the way that
switch
works in other languages. -
The body of a
match
arm can be a single expression or a block. Technically this is the same thing, since blocks are also expressions, but students may not fully understand that symmetry at this point. -
match
expressions need to be exhaustive, meaning they either need to cover all possible values or they need to have a default case such as_
. Exhaustiveness is easiest to demonstrate with enums, but enums haven't been introduced yet. Instead we demonstrate matching on abool
, which is the simplest primitive type. -
This slide introduces
match
without talking about pattern matching, giving students a chance to get familiar with the syntax without front-loading too much information. We'll be talking about pattern matching in more detail tomorrow, so try not to go into too much detail here.
More to Explore
-
To further motivate the usage of
match
, you can compare the examples to their equivalents written withif
. In the second case matching on abool
anif {} else {}
block is pretty similar. But in the first example that checks multiple cases, amatch
expression can be more concise thanif {} else if {} else if {} else
. -
match
also supports match guards, which allow you to add an arbitrary logical condition that will get evaluated to determine if the match arm should be taken. However talking about match guards requires explaining about pattern matching, which we're trying to avoid on this slide.