What if you could check many things at once with just one simple line?
Why Matching multiple patterns in Rust? - Purpose & Use Cases
Imagine you have a list of fruits and you want to check if each fruit is either an apple, a banana, or an orange. Without matching multiple patterns, you would write separate checks for each fruit one by one.
This manual way means writing repetitive code for each fruit type. It gets long, hard to read, and easy to make mistakes like forgetting a fruit or mixing conditions.
Matching multiple patterns lets you check several options in one clean line. It groups similar cases together, making your code shorter, clearer, and less error-prone.
if fruit == "apple" || fruit == "banana" || fruit == "orange" { println!("It's a common fruit!"); }
match fruit {
"apple" | "banana" | "orange" => println!("It's a common fruit!"),
_ => println!("Other fruit"),
}This lets you handle many similar cases together easily, making your programs simpler and faster to write.
Think about a vending machine that accepts multiple coin types. Matching multiple patterns helps check if the inserted coin is one of the accepted types in a neat way.
Manual checks for many options are repetitive and error-prone.
Matching multiple patterns groups cases for cleaner code.
It makes your programs easier to read and maintain.
Practice
match arm?Solution
Step 1: Recall the syntax for matching multiple patterns
Rust uses the vertical bar|to separate multiple patterns in one match arm.Step 2: Identify the correct symbol
The symbol|means "or" in pattern matching, allowing multiple cases to be handled together.Final Answer:
| -> Option AQuick Check:
Multiple patterns use | in match arms [OK]
- Using & instead of |
- Confusing + with pattern separator
- Using && which is logical AND, not pattern OR
match arms correctly matches the values 1, 2, or 3 using multiple patterns?Solution
Step 1: Check the syntax for multiple patterns
Rust uses|to separate multiple patterns in a match arm, so1 | 2 | 3is correct.Step 2: Identify incorrect options
&is not used for matching multiple patterns,+is arithmetic, and commas are invalid here.Final Answer:
1 | 2 | 3 => println!("Matched 1, 2, or 3"), -> Option BQuick Check:
Multiple patterns use | separator [OK]
- Using & instead of | for multiple patterns
- Trying to separate patterns with commas
- Using + which is arithmetic, not pattern matching
let x = 2;
match x {
1 | 2 | 3 => println!("Small number"),
4 | 5 | 6 => println!("Medium number"),
_ => println!("Other number"),
}Solution
Step 1: Identify which pattern matches x=2
The first arm matches 1, 2, or 3. Since x is 2, it matches this arm.Step 2: Determine the output
The matched arm prints "Small number" to the console.Final Answer:
Small number -> Option CQuick Check:
2 matches 1|2|3 arm [OK]
- Choosing Medium number for 2
- Thinking _ arm matches first
- Assuming syntax error due to multiple patterns
let ch = 'a';
match ch {
'a' | 'e' | 'i' | 'o' | 'u' => println!("Vowel"),
'a' | 'b' => println!("Letter a or b"),
_ => println!("Other"),
}Solution
Step 1: Identify duplicate patterns in match arms
The character 'a' appears in both the first and second arms, causing the second arm's 'a' pattern to be unreachable.Step 2: Understand Rust's pattern matching rules
Rust does not allow unreachable patterns; the compiler will warn or error about the duplicate 'a'.Final Answer:
Duplicate pattern 'a' in multiple arms causes unreachable code -> Option DQuick Check:
Duplicate patterns cause unreachable code errors [OK]
- Ignoring unreachable pattern warnings
- Thinking | is invalid for multiple patterns
- Missing default arm (actually present here)
match arm correctly uses multiple patterns to do this?fn day_type(day: &str) -> &str {
match day {
? => "Weekend",
? => "Weekday",
_ => "Invalid",
}
}Solution
Step 1: Use | to match multiple string patterns in one arm
Rust allows multiple patterns separated by|to group cases like "Saturday" | "Sunday".Step 2: Check other options for invalid syntax
Commas separate arms, not patterns; & and + are invalid for pattern matching.Final Answer:
"Saturday" | "Sunday", "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" -> Option AQuick Check:
Use | to combine multiple string patterns [OK]
- Using commas instead of | inside match arms
- Using & or + which are invalid for patterns
- Forgetting to cover all weekdays or weekend days
Ready to prove it?
Pass the test and this pattern moves unknown
