Assignment operators in Rust - Time & Space Complexity
Assignment operators update values in variables. We want to see how the time to do this changes as the input grows.
How does the number of assignments affect the total work done?
Analyze the time complexity of the following code snippet.
let mut sum = 0;
for i in 1..=n {
sum += i; // assignment operator +=
}
println!("Sum: {}", sum);
This code adds numbers from 1 to n using the += assignment operator inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The += assignment inside the loop.
- How many times: Exactly n times, once for each number from 1 to n.
Each time n grows, the loop runs more times, doing one assignment each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The work grows directly with n; double n means double the assignments.
Time Complexity: O(n)
This means the time to complete the assignments grows in a straight line with the input size.
[X] Wrong: "Assignment operators like += are instant and don't add to time."
[OK] Correct: Each assignment inside a loop runs every time, so many assignments add up and affect total time.
Understanding how simple assignments inside loops add up helps you explain how code runs as input grows. This skill shows you can think about efficiency clearly.
"What if we replaced the loop with a recursive function doing the same += assignment? How would the time complexity change?"
Practice
+= do?Solution
Step 1: Understand the
The+=operator+=operator adds the right-hand side value to the left-hand side variable.Step 2: Effect on the variable
The result of the addition is stored back into the same variable, updating its value.Final Answer:
Adds the right value to the left variable and stores the result in the variable -> Option BQuick Check:
+=means add and assign [OK]
- Confusing += with subtraction or multiplication
- Forgetting that the variable must be mutable
- Thinking it creates a new variable instead of updating
*= operator in Rust?Solution
Step 1: Check variable mutability
In Rust, to change a variable's value, it must be declared withmut. Solet mut x = 5;is correct.Step 2: Correct operator usage
The operator*=is used asx *= 2;to multiply and assign.Final Answer:
let mut x = 5; x *= 2; -> Option DQuick Check:
Mutable variable + operator syntax = correct [OK]
- Not declaring variable as mutable
- Placing
mutin wrong position - Incorrect operator syntax like
*= x 2
let mut a = 10;
a += 5;
a *= 2;
println!("{}", a);Solution
Step 1: Perform
Starting witha += 5;a = 10, adding 5 givesa = 15.Step 2: Perform
Multiplying 15 by 2 updatesa *= 2;a = 30.Final Answer:
30 -> Option AQuick Check:
10 + 5 = 15; 15 * 2 = 30 [OK]
- Adding and multiplying in wrong order
- Forgetting variable is mutable
- Misreading operator effects
let x = 8;
x -= 3;
println!("{}", x);Solution
Step 1: Check variable mutability
The variablexis declared withoutmut, so it is immutable.Step 2: Check assignment operator usage
The operator-=tries to subtract and assign a new value tox, which is not allowed for immutable variables.Final Answer:
Variable x is not mutable but is modified -> Option CQuick Check:
Immutable variable cannot be changed [OK]
- Assuming all variables are mutable by default
- Thinking operator syntax is wrong
- Ignoring Rust's immutability rules
result?let mut result = 50; let change = 7; result %= change; result += 3; result *= 4;
Solution
Step 1: Calculate
50 % 7 equals 1 because 7 * 7 = 49 and remainder is 1, soresult %= change;result = 1.Step 2: Calculate
Add 3 to 1 givesresult += 3;result = 4.Step 3: Calculate
Multiply 4 by 4 givesresult *= 4;result = 16.Final Answer:
16 -> Option AQuick Check:
50 % 7 = 1; 1 + 3 = 4; 4 * 4 = 16 [OK]
- Miscomputing modulus operation
- Skipping steps or doing operations out of order
- Choosing wrong multiplication result
Ready to prove it?
Pass the test and this pattern moves unknown
