Matching multiple patterns lets you check if a value fits any of several options in one place. It keeps your code clean and easy to read.
When you want to run the same code for different possible values.
When you have a list of options that should be handled the same way.
When you want to simplify long if-else chains checking many values.
When you want to group similar cases in a match statement.
When you want to avoid repeating code for each similar pattern.
Syntax
Rust
match value {
pattern1 | pattern2 | pattern3 => {
// code for any of these patterns
},
_ => {
// code for other cases
}
}
You use the pipe symbol | to separate multiple patterns.
All patterns separated by | share the same code block.
Examples
This matches if x is 1, 2, or 3 and prints the same message.
Rust
let x = 3;
match x {
1 | 2 | 3 => println!("x is 1, 2, or 3"),
_ => println!("x is something else"),
}
This matches vowels and prints "vowel" for any of them.
Rust
let c = 'a';
match c {
'a' | 'e' | 'i' | 'o' | 'u' => println!("vowel"),
_ => println!("consonant"),
}
Sample Program
This program checks the number for a day of the week. It prints if it's a weekday or weekend using multiple patterns.
Rust
fn main() {
let day = 6;
match day {
1 | 2 | 3 | 4 | 5 => println!("It's a weekday."),
6 | 7 => println!("It's the weekend!"),
_ => println!("Not a valid day."),
}
}
OutputSuccess
Important Notes
You can mix literal values and variables in patterns when matching multiple options.
Using | helps avoid repeating the same code for similar cases.
Summary
Use | to match multiple patterns in one match arm.
This keeps your code shorter and easier to understand.
It works well for grouping similar cases together.
Practice
(1/5)
1. In Rust, which symbol is used to match multiple patterns in a single match arm?
easy
A. |
B. &
C. +
D. &&
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 A
Quick Check:
Multiple patterns use | in match arms [OK]
Hint: Use | to combine patterns in one match arm [OK]
Common Mistakes:
Using & instead of |
Confusing + with pattern separator
Using && which is logical AND, not pattern OR
2. Which of the following Rust match arms correctly matches the values 1, 2, or 3 using multiple patterns?
easy
A. 1 & 2 & 3 => println!("Matched 1, 2, or 3"),
B. 1 | 2 | 3 => println!("Matched 1, 2, or 3"),
C. 1 + 2 + 3 => println!("Matched 1, 2, or 3"),
D. 1, 2, 3 => println!("Matched 1, 2, or 3"),
Solution
Step 1: Check the syntax for multiple patterns
Rust uses | to separate multiple patterns in a match arm, so 1 | 2 | 3 is 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 B
Quick Check:
Multiple patterns use | separator [OK]
Hint: Separate patterns with | inside match arms [OK]
Common Mistakes:
Using & instead of | for multiple patterns
Trying to separate patterns with commas
Using + which is arithmetic, not pattern matching
3. What will be the output of this Rust code?
let x = 2;
match x {
1 | 2 | 3 => println!("Small number"),
4 | 5 | 6 => println!("Medium number"),
_ => println!("Other number"),
}
medium
A. Compilation error
B. Medium number
C. Small number
D. 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 C
Quick Check:
2 matches 1|2|3 arm [OK]
Hint: Check which pattern includes the value [OK]
Common Mistakes:
Choosing Medium number for 2
Thinking _ arm matches first
Assuming syntax error due to multiple patterns
4. Find the error in this Rust code snippet that tries to match multiple patterns:
let ch = 'a';
match ch {
'a' | 'e' | 'i' | 'o' | 'u' => println!("Vowel"),
'a' | 'b' => println!("Letter a or b"),
_ => println!("Other"),
}
medium
A. Missing semicolon after println! macros
B. Match expression missing a default arm
C. Incorrect use of | operator for multiple patterns
D. Duplicate pattern 'a' in multiple arms causes unreachable code
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 D
Quick Check:
Duplicate patterns cause unreachable code errors [OK]
Hint: Avoid repeating same pattern in multiple match arms [OK]
Common Mistakes:
Ignoring unreachable pattern warnings
Thinking | is invalid for multiple patterns
Missing default arm (actually present here)
5. You want to write a Rust function that returns "Weekend" for Saturday or Sunday, "Weekday" for Monday to Friday, and "Invalid" otherwise. Which match arm correctly uses multiple patterns to do this?
fn day_type(day: &str) -> &str {
match day {
? => "Weekend",
? => "Weekday",
_ => "Invalid",
}
}