If–else expression in Rust - Time & Space Complexity
We want to see how the time it takes to run an if-else expression changes as the input changes.
Specifically, does choosing one path or the other affect how long the program runs?
Analyze the time complexity of the following code snippet.
fn check_number(n: i32) -> &'static str {
if n > 0 {
"Positive"
} else {
"Non-positive"
}
}
This code checks if a number is positive or not and returns a message accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check.
- How many times: Exactly once per function call.
The program does one simple check no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same no matter how big the input number is.
Time Complexity: O(1)
This means the time to run the if-else expression does not grow with the input size; it stays constant.
[X] Wrong: "If-else expressions take longer when the input number is bigger."
[OK] Correct: The if-else only checks one condition once, so the input size does not affect how long it takes.
Understanding that simple decisions like if-else checks run in constant time helps you explain how your code behaves clearly and confidently.
"What if we added a loop inside the if or else block? How would the time complexity change?"
Practice
if-else expression in Rust do?Solution
Step 1: Understand the purpose of if-else
An if-else expression lets the program pick one of two paths based on a condition.Step 2: Recognize that it returns a value
In Rust, if-else expressions return a value that can be stored in a variable.Final Answer:
Chooses between two options and returns a value -> Option DQuick Check:
If-else expression = choice with value [OK]
- Thinking if-else repeats code like a loop
- Confusing if-else with function definition
- Ignoring that if-else returns a value
Solution
Step 1: Check Rust if-else syntax
Rust usesif condition { value1 } else { value2 }with braces and no parentheses.Step 2: Identify correct option
let x = if condition { 5 } else { 10 }; matches Rust syntax exactly with variable assignment and braces.Final Answer:
let x = if condition { 5 } else { 10 }; -> Option AQuick Check:
Rust if-else syntax = let x = if condition { 5 } else { 10 }; [OK]
- Using parentheses around condition
- Writing elseif instead of else if
- Using colons instead of braces
let number = 7;
let result = if number % 2 == 0 { "even" } else { "odd" };
println!("{}", result);Solution
Step 1: Check the condition number % 2 == 0
7 % 2 equals 1, which is not 0, so condition is false.Step 2: Determine which branch runs
Since condition is false, the else branch runs, returning "odd".Final Answer:
odd -> Option AQuick Check:
7 is odd, so output = odd [OK]
- Assuming 7 is even
- Confusing modulo operator
- Expecting number to print instead of result
let x = 10;
let y = if x > 5 { 1 };
println!("{}", y);Solution
Step 1: Check if-else expression completeness
When assigning with if, Rust requires both if and else branches to return a value.Step 2: Identify missing else branch
The code has no else branch, so y's value is undefined, causing a compile error.Final Answer:
Missing else branch in if expression -> Option CQuick Check:
if-else must have else when used as expression [OK]
- Forgetting else branch when assigning
- Thinking if alone returns a value
- Misreading error as println! issue
grade based on a score: if score is 90 or above, grade is 'A', else 'F'. Which code correctly uses if-else expression?Solution
Step 1: Check correct if-else expression syntax
Rust usesif condition { value } else { value }without 'then' or semicolons inside.Step 2: Validate condition and branches
let grade = if score >= 90 { 'A' } else { 'F' }; correctly checks >= 90 and returns 'A' or 'F' in else branch.Final Answer:
let grade = if score >= 90 { 'A' } else { 'F' }; -> Option BQuick Check:
Correct syntax and logic = let grade = if score >= 90 { 'A' } else { 'F' }; [OK]
- Using 'then' keyword
- Missing else branch
- Incorrect semicolon placement
Ready to prove it?
Pass the test and this pattern moves unknown
