Advanced Pattern Matching
Medium
+3 pts
Intro to Rust
14/15
🎯 Python 3.10 introduced structural pattern matching:
match command:
case {"action": "move", "x": x, "y": y}:
print(f"Moving to {x}, {y}")
case {"action": "quit"}:
print("Goodbye")
Rust's match goes further with features you'll use constantly: ranges, multiple patterns, guards, destructuring, and exhaustiveness checking. This exercise drills all of them.
Ranges and multiple patterns
match score {
90..=100 => "A", // range pattern
75..=89 | 70..=74 => "B/C", // multiple patterns with |
_ => "below", // catch-all
}
Python's match doesn't support ranges directly — you'd use case x if 90 <= x <= 100. Rust makes it first-class syntax.
Guards
Guards add conditions to patterns:
match age {
n if n >= 18 => "adult",
13..=17 => "teen",
_ => "child",
}
Python has guards too (case …
Login to see the full exercise.