Match overview in Rust - Time & Space Complexity
We want to understand how the time a Rust match statement takes changes as the number of patterns grows.
Specifically, how does checking many cases affect the work done?
Analyze the time complexity of the following code snippet.
fn describe_number(num: i32) -> &'static str {
match num {
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
_ => "other",
}
}
This code matches a number against several fixed cases and returns a word for it or "other" if no match.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each pattern in the
matchuntil one fits. - How many times: Up to the number of patterns (here 6 including the catch-all).
As the number of patterns increases, the time to find a match grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The work grows directly with the number of patterns to check.
Time Complexity: O(n)
This means the time to find a match grows linearly as you add more patterns.
[X] Wrong: "A match always runs in constant time no matter how many patterns there are."
[OK] Correct: The match checks patterns one by one until it finds a match, so more patterns usually mean more checks.
Understanding how pattern matching scales helps you reason about code efficiency and choose better structures when needed.
"What if the match patterns were replaced by a hash map lookup? How would the time complexity change?"
Practice
match statement in Rust?Solution
Step 1: Understand the role of
Thematchmatchstatement compares a value against many patterns, making code clearer than manyif-elsestatements.Step 2: Compare with other options
Loops run repeatedly, data types define structures, and exceptions handle errors, butmatchis for pattern matching.Final Answer:
To check a value against multiple patterns clearly and concisely -> Option AQuick Check:
Match = pattern checking [OK]
- Confusing match with loops
- Thinking match defines types
- Assuming match handles exceptions
match expression in Rust?Solution
Step 1: Recall Rust match syntax
Rust uses curly braces with patterns followed by => and expressions separated by commas.Step 2: Check each option
match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr } uses correct braces, arrows, and commas. Options B and D use semicolons incorrectly. match value { pattern1 -> expr1, pattern2 -> expr2, _ -> default_expr } uses -> instead of =>.Final Answer:
match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr } -> Option AQuick Check:
Correct match syntax uses => and commas [OK]
- Using semicolons instead of commas
- Using -> instead of =>
- Missing the catch-all _ pattern
let number = 3;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Other"),
}Solution
Step 1: Identify the matched value
The variablenumberis 3, so the match will look for pattern 3.Step 2: Find the matching arm
The arm3 => println!("Three")matches and prints "Three".Final Answer:
Three -> Option DQuick Check:
number=3 matches 3 => "Three" [OK]
- Choosing default _ arm instead of exact match
- Confusing numbers with strings
- Ignoring the value of number
match code snippet:let x = 5;
match x {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three")
}Solution
Step 1: Check match arms completeness
The match covers 1, 2, and 3 but does not handle other values like 5.Step 2: Understand Rust match requirements
Rust requires all possible values to be covered, usually by a catch-all_pattern.Final Answer:
Missing catch-all_pattern to handle other values -> Option CQuick Check:
Always include _ to cover unmatched cases [OK]
- Forgetting the catch-all _ pattern
- Thinking semicolons are mandatory after println!
- Confusing braces with parentheses
let grade = 'B';
match grade {
'A' | 'B' | 'C' => println!("Pass"),
'D' => println!("Conditional Pass"),
_ => println!("Fail"),
}Solution
Step 1: Identify the value and patterns
The variablegradeis 'B'. The first arm matches 'A', 'B', or 'C'.Step 2: Determine which arm runs
Since 'B' matches the first arm, it prints "Pass".Final Answer:
Pass -> Option BQuick Check:
Multiple patterns with | match 'B' => "Pass" [OK]
- Assuming only single pattern per arm
- Choosing wrong output for 'B'
- Thinking code causes compilation error
Ready to prove it?
Pass the test and this pattern moves unknown
