Curriculum Data modeling Pattern matching
Pattern matching
match is not a switch statement with nicer syntax. It destructures, binds names, checks exhaustiveness, and returns values. Planned exercises: 1. choose exhaustive match. 2. recognise wildcard _. 3. choose binding inside a variant. 4. fill a match arm. 5. fill an if let for one interesting case. 6. write a match returning a string. 7. write destructuring for a struct variant. 8. translate TS discriminant checks. 9. fix a non-exhaustive match.
Exercises
9 ready
01
pick one
Given enum Sign { Pos, Neg, Zero }, pick the match that
02
pick one
A match handles two specific variants explicitly and wants a
03
pick one
Given enum Msg { Echo(String) }, pick the arm that BINDS the
04
type one line
Fill the missing match arm. Destructure Outcome::Err(reason)
05
type one line
When you only care about ONE variant, if let is the shorthand
06
write a program
Define enum Direction { North, South, East, West }. Write
07
write a program
Define an enum Shape with a struct-style variant
08
write a program
Translate the TS event union to Rust. Event has three
09
write a program
This program fails with `error[E0004]: non-exhaustive patterns: