Operator precedence in Rust - Time & Space Complexity
When we use operators in Rust, the order they run matters. This order is called operator precedence.
We want to see how this order affects how many steps the program takes.
Analyze the time complexity of the following code snippet.
fn calculate(x: i32, y: i32, z: i32) -> i32 {
let result = x + y * z - 5 / 2;
result
}
This code calculates a value using addition, multiplication, subtraction, and division in one line.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated steps here; just a few math operations done once.
- How many times: Each operator runs exactly once per function call.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 |
| 100 | 4 |
| 1000 | 4 |
Pattern observation: The number of operations stays the same no matter how big the inputs are.
Time Complexity: O(1)
This means the program takes the same amount of time no matter the input size.
[X] Wrong: "More operators mean the program takes longer as inputs grow."
[OK] Correct: The operators run a fixed number of times, so input size does not change the steps.
Understanding operator precedence helps you read and write code that runs efficiently and correctly, a skill useful in many programming tasks.
"What if we replaced the single calculation with a loop repeating it n times? How would the time complexity change?"
Practice
+, *, -, /?Solution
Step 1: Recall Rust operator precedence rules
Multiplication (*) and division (/) operators have higher precedence than addition (+) and subtraction (-).Step 2: Compare given operators
Among the given operators, * and / are evaluated before + and - in expressions.Final Answer:
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). -> Option AQuick Check:
*, / > +, - [OK]
- Thinking addition has higher precedence than multiplication
- Assuming all operators have equal precedence
- Confusing subtraction precedence with multiplication
Solution
Step 1: Understand operator precedence and parentheses
Multiplication (*) has higher precedence than addition (+), so without parentheses, multiplication happens first.Step 2: Check each expression
(2 + 3) * 4uses parentheses to add 2 and 3 first, then multiplies by 4, which matches the requirement.Final Answer:
(2 + 3) * 4 -> Option BQuick Check:
Parentheses force addition first [OK]
- Ignoring parentheses and relying on default precedence
- Misplacing parentheses causing wrong order
- Assuming left-to-right always applies
let result = 10 - 4 / 2 + 3 * 2;
println!("{}", result);Solution
Step 1: Apply operator precedence to expression
Division and multiplication happen before addition and subtraction:
4 / 2 = 2
3 * 2 = 6Step 2: Calculate left to right with results
Expression becomes: 10 - 2 + 6
10 - 2 = 8
8 + 6 = 14Final Answer:
14 -> Option DQuick Check:
*/ before +- gives 14 [OK]
- Doing operations strictly left to right ignoring precedence
- Adding before multiplying
- Miscomputing division or multiplication
let value = 5 + 3 * / 2;
Solution
Step 1: Analyze the expression syntax
The expression has* /operators together without a number between them, which is invalid syntax in Rust.Step 2: Confirm error type
This causes a syntax error because Rust expects an operand between operators.Final Answer:
Syntax error due to misplaced operator '/' -> Option CQuick Check:
Operators must have operands between them [OK]
- Assuming expression is valid without checking syntax
- Confusing syntax error with runtime error
- Ignoring operator placement rules
let x = 2 + 3 * 4 - 6 / 3 + (8 - 5) * 2;
What is the correct value of
x after evaluation?Solution
Step 1: Evaluate parentheses and high precedence operators
Calculate inside parentheses:
(8 - 5) = 3
Then multiplication and division:
3 * 4 = 12
6 / 3 = 2
3 * 2 = 6 (from parentheses result * 2)Step 2: Substitute and calculate addition and subtraction left to right
Expression becomes: 2 + 12 - 2 + 6
2 + 12 = 14
14 - 2 = 12
12 + 6 = 18Final Answer:
18 -> Option AQuick Check:
Parentheses and */ first, then +- left to right [OK]
- Ignoring parentheses and calculating in wrong order
- Doing addition before multiplication
- Mixing up left-to-right evaluation for + and -
Ready to prove it?
Pass the test and this pattern moves unknown
