A destructuring pattern in Rust is a way to break down complex data types like tuples, structs, or enums into their parts, making it easier to access individual values.
Click to reveal answer
beginner
How do you destructure a tuple in Rust?
You use parentheses with variable names matching the tuple's structure. For example: <code>let (x, y) = (1, 2);</code> assigns 1 to x and 2 to y.
Click to reveal answer
beginner
What does the underscore (_) mean in a destructuring pattern?
The underscore (_) is a wildcard that ignores a value in a pattern. It tells Rust you don't care about that part and don't want to bind it to a variable.
Click to reveal answer
intermediate
How can you destructure a struct in Rust?
You use curly braces with field names. For example: <code>let Point { x, y } = point;</code> extracts the x and y fields from the struct named point.
Click to reveal answer
intermediate
What is the benefit of using destructuring patterns in Rust?
Destructuring makes code cleaner and easier to read by directly extracting needed parts of data. It avoids manual indexing or calling getters, reducing errors.
Click to reveal answer
What does this Rust code do? let (a, b) = (5, 10);
ACauses a syntax error
BCreates a tuple (a, b) with values 5 and 10
CSwaps the values of a and b
DAssigns 5 to a and 10 to b
✗ Incorrect
The code destructures the tuple (5, 10) and assigns 5 to a and 10 to b.
In a pattern, what does the underscore (_) do?
ABinds the value to a variable named _
BCauses a compile error
CIgnores the value without binding
DDuplicates the value
✗ Incorrect
The underscore is a wildcard that ignores the value without creating a variable.
How do you destructure a struct named Point with fields x and y?
Alet Point { x, y } = point;
Blet Point(x, y) = point;
Clet (x, y) = point;
Dlet {x, y} = point;
✗ Incorrect
Struct destructuring uses curly braces with field names: let Point { x, y } = point;
Which of these is NOT a valid destructuring pattern in Rust?
Alet (a, b) = tuple;
Blet (x; y) = tuple;
Clet Struct { x, y } = s;
Dlet [x, y] = array;
✗ Incorrect
The semicolon inside parentheses is invalid syntax for destructuring.
Why use destructuring patterns in Rust?
ATo directly access parts of data easily
BTo slow down the program
CTo avoid using variables
DTo make code longer and complex
✗ Incorrect
Destructuring helps access parts of data directly, making code cleaner and easier.
Explain how destructuring patterns work in Rust with examples for tuples and structs.
Think about how you can break down data into parts.
You got /4 concepts.
Describe the role of the underscore (_) in destructuring patterns and why it is useful.
It's like saying 'I don't care about this part'.
You got /3 concepts.
Practice
(1/5)
1. What does destructuring a tuple in Rust allow you to do?
easy
A. Change the tuple's type
B. Convert the tuple into a string
C. Break the tuple into individual variables easily
D. Sort the elements inside the tuple
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 C
Quick Check:
Destructuring = Break tuple into variables [OK]
Hint: Destructuring splits compound data into parts fast [OK]
Common Mistakes:
Thinking destructuring changes data type
Confusing destructuring with conversion
Assuming destructuring sorts data
2. Which of the following is the correct syntax to destructure a tuple (x, y) into variables a and b in Rust?
easy
A. let (a, b) = (x, y);
B. let [a, b] = (x, y);
C. let {a, b} = (x, y);
D. let a, b = (x, y);
Solution
Step 1: Recall tuple destructuring syntax
Rust uses parentheses to destructure tuples, matching the tuple structure.
Step 2: Match variables to tuple elements
Using let (a, b) = (x, y); correctly assigns a to x and b to y.
Final Answer:
let (a, b) = (x, y); -> Option A
Quick Check:
Tuple destructuring uses parentheses [OK]
Hint: Use parentheses to destructure tuples in let statements [OK]
Common Mistakes:
Using square brackets instead of parentheses
Using curly braces for tuples
Trying to assign without parentheses
3. What will be the output of this Rust code?
let point = (3, 7); let (x, y) = point; println!("x = {}, y = {}", x, y);
medium
A. x = 3, y = 7
B. x = 7, y = 3
C. x = (3, 7), y = (3, 7)
D. Compilation error
Solution
Step 1: Destructure tuple values
The tuple point has values (3, 7). Destructuring assigns x = 3 and y = 7.
Step 2: Print the variables
The println! macro outputs the values of x and y in order.
Final Answer:
x = 3, y = 7 -> Option A
Quick Check:
Destructured values print as assigned [OK]
Hint: Destructure tuple to variables, then print them [OK]
Common Mistakes:
Swapping values in destructuring
Printing the whole tuple instead of variables
Expecting syntax error due to destructuring
4. Identify the error in this Rust code snippet:
let (a, b) = (1, 2, 3);
medium
A. Missing semicolon at the end
B. Variables a and b are not mutable
C. Tuple syntax is incorrect
D. Too many values to destructure into two variables
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 D
Quick Check:
Variable count must match tuple length [OK]
Hint: Match variable count to tuple elements exactly [OK]
Common Mistakes:
Ignoring tuple length mismatch
Assuming Rust auto-fills missing variables
Thinking mutability causes error here
5. Given the enum and code below, what is the value of result?