Matching multiple patterns in Rust - Time & Space Complexity
When we match multiple patterns in Rust, we want to know how long it takes as the input changes.
We ask: How does the time grow when we check many patterns?
Analyze the time complexity of the following code snippet.
fn check_number(num: i32) {
match num {
1 | 2 | 3 => println!("Small number"),
4 | 5 | 6 => println!("Medium number"),
_ => println!("Other number"),
}
}
This code checks if a number matches any of several patterns grouped with | (or).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the input against each pattern in order.
- How many times: Up to the number of patterns until a match is found.
As the number of patterns grows, the checks grow too, but only until a match is found.
| Input Size (patterns) | Approx. Operations |
|---|---|
| 3 | Up to 3 checks |
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
Pattern observation: The number of checks grows linearly with the number of patterns.
Time Complexity: O(n)
This means the time to match grows in a straight line as you add more patterns.
[X] Wrong: "Matching multiple patterns happens all at once, so time stays the same no matter how many patterns."
[OK] Correct: Rust checks patterns one by one until it finds a match, so more patterns mean more checks in the worst case.
Understanding how pattern matching time grows helps you write clear and efficient code, a skill valued in many coding challenges and real projects.
"What if we changed the patterns to use ranges instead of individual values? How would the time complexity change?"
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
