Basic match usage in Rust - Time & Space Complexity
Let's see how the time it takes to run a simple match statement changes as the input changes.
We want to know how many steps the program does when it checks different values.
Analyze the time complexity of the following code snippet.
fn describe_number(num: i32) -> &'static str {
match num {
1 => "one",
2 => "two",
3 => "three",
_ => "other",
}
}
This code checks a number and returns a word for 1, 2, or 3, or "other" if it is something else.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The match statement compares the input number to fixed cases.
- How many times: It checks each case once until it finds a match or reaches the default.
Each time the function runs, it checks a few fixed cases no matter what number you give it.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | Checks up to 1 case |
| 2 | Checks up to 2 cases |
| 10 | Checks up to 3 cases then default |
Pattern observation: The number of checks stays small and does not grow with input size.
Time Complexity: O(1)
This means the time to run does not grow as the input number gets bigger.
[X] Wrong: "The match will take longer if the number is bigger."
[OK] Correct: The match only checks a few fixed cases, so it does not depend on how big the number is.
Understanding that simple match statements run in constant time helps you explain how your code handles choices quickly and clearly.
"What if we added 100 more cases to the match? How would the time complexity change?"
Practice
match expression do in Rust?Solution
Step 1: Understand the purpose of
Thematchmatchexpression compares a value against several patterns.Step 2: Identify what happens on a match
When a pattern matches, the corresponding code block runs.Final Answer:
It compares a value against multiple patterns and runs matching code. -> Option AQuick Check:
match checks patterns = C [OK]
- Thinking match creates variables
- Confusing match with loops
- Believing match defines functions
match expression in Rust?Solution
Step 1: Recall Rust match syntax
Rust uses curly braces with patterns and arrows:{ pattern => action }.Step 2: Check each option's syntax
match value { pattern => action, _ => default_action } matches Rust syntax exactly; others use wrong symbols or parentheses.Final Answer:
match value { pattern => action, _ => default_action } -> Option CQuick Check:
Correct match syntax = A [OK]
- Using parentheses instead of braces
- Using -> instead of =>
- Using colons instead of arrows
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.Step 2: Find matching pattern in match arms
The pattern3matches, so it prints "Three".Final Answer:
Three -> Option DQuick Check:
number=3 matches 3 => "Three" [OK]
- Choosing default (_) arm instead of exact match
- Confusing numbers in patterns
- Ignoring the value of the variable
let x = 5;
match x {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three")
}Solution
Step 1: Check match arms coverage
The match covers only 1, 2, and 3 but variable is 5.Step 2: Identify missing default arm
Without a_arm, the code will not compile because not all values are covered.Final Answer:
Missing default (_) arm to cover other values -> Option BQuick Check:
All values must be matched or _ arm added [OK]
- Assuming semicolons are required after println!
- Ignoring missing default arm errors
- Confusing variable names
match expression that returns a string describing a number: "small" for 1 or 2, "medium" for 3 or 4, and "large" for any other number. Which code correctly uses match to do this?Solution
Step 1: Understand pattern alternatives in match
Rust uses|to match multiple patterns in one arm.Step 2: Check each option's pattern syntax
match num { 1 | 2 => "small", 3 | 4 => "medium", _ => "large", } correctly uses1 | 2and3 | 4. Others use invalid syntax like commas, ||, or arrays.Final Answer:
match num { 1 | 2 => "small", 3 | 4 => "medium", _ => "large", } -> Option AQuick Check:
Use | for multiple patterns in match arms [OK]
- Using commas instead of | in patterns
- Using || which is logical OR, not pattern OR
- Trying to match arrays instead of values
Ready to prove it?
Pass the test and this pattern moves unknown
