Destructuring patterns in Rust - Time & Space Complexity
We want to understand how the time it takes to run code with destructuring patterns changes as the input grows.
Specifically, we ask: how does the cost of breaking apart data with destructuring grow with input size?
Analyze the time complexity of the following code snippet.
fn sum_points(points: &[(i32, i32)]) -> i32 {
let mut total = 0;
for &(x, y) in points {
total += x + y;
}
total
}
This code sums all the x and y values from a list of point pairs using destructuring in the loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each point and destructuring the tuple into x and y.
- How many times: Once for each point in the input list.
As the number of points grows, the code does more work by repeating the sum for each point.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 sums and destructures |
| 100 | About 100 sums and destructures |
| 1000 | About 1000 sums and destructures |
Pattern observation: The work grows directly with the number of points, so doubling points doubles work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of points.
[X] Wrong: "Destructuring makes the code slower because it adds extra steps inside the loop."
[OK] Correct: Destructuring is just a way to name parts of the data and happens as part of the loop step, so it does not add extra loops or nested work.
Understanding how destructuring affects time helps you explain your code clearly and shows you know how data access scales with input size.
"What if we changed the input to a nested list of points instead of a flat list? How would the time complexity change?"
Practice
Solution
Step 1: Understand tuple structure
A tuple groups multiple values together as one compound value.Step 2: Apply destructuring
Destructuring lets you assign each element of the tuple to its own variable easily.Final Answer:
Break the tuple into individual variables easily -> Option CQuick Check:
Destructuring = Break tuple into variables [OK]
- Thinking destructuring changes data type
- Confusing destructuring with conversion
- Assuming destructuring sorts data
(x, y) into variables a and b in Rust?Solution
Step 1: Recall tuple destructuring syntax
Rust uses parentheses to destructure tuples, matching the tuple structure.Step 2: Match variables to tuple elements
Usinglet (a, b) = (x, y);correctly assignsatoxandbtoy.Final Answer:
let (a, b) = (x, y); -> Option AQuick Check:
Tuple destructuring uses parentheses [OK]
- Using square brackets instead of parentheses
- Using curly braces for tuples
- Trying to assign without parentheses
let point = (3, 7);
let (x, y) = point;
println!("x = {}, y = {}", x, y);
Solution
Step 1: Destructure tuple values
The tuplepointhas values (3, 7). Destructuring assignsx = 3andy = 7.Step 2: Print the variables
Theprintln!macro outputs the values ofxandyin order.Final Answer:
x = 3, y = 7 -> Option AQuick Check:
Destructured values print as assigned [OK]
- Swapping values in destructuring
- Printing the whole tuple instead of variables
- Expecting syntax error due to destructuring
let (a, b) = (1, 2, 3);
Solution
Step 1: Check tuple length vs variables
The tuple has 3 values but only 2 variables are provided for destructuring.Step 2: Understand destructuring rules
Rust requires the number of variables to match the tuple elements exactly.Final Answer:
Too many values to destructure into two variables -> Option DQuick Check:
Variable count must match tuple length [OK]
- Ignoring tuple length mismatch
- Assuming Rust auto-fills missing variables
- Thinking mutability causes error here
result?enum Shape {
Circle(f64),
Rectangle { width: f64, height: f64 },
}
let shape = Shape::Rectangle { width: 3.0, height: 4.0 };
let result = match shape {
Shape::Circle(radius) => radius * radius * 3.14,
Shape::Rectangle { width, height } => width * height,
};Solution
Step 1: Identify the variant matched
The variableshapeis aRectanglewith width 3.0 and height 4.0.Step 2: Calculate area using destructured values
The match arm forRectanglemultiplies width and height: 3.0 * 4.0 = 12.0.Final Answer:
12.0 -> Option BQuick Check:
Rectangle area = width * height = 12.0 [OK]
- Confusing Circle and Rectangle variants
- Using radius formula for Rectangle
- Forgetting to destructure fields properly
Ready to prove it?
Pass the test and this pattern moves unknown
