Bird
Raised Fist0
Rustprogramming~20 mins

Destructuring patterns in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Destructuring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this tuple destructuring?

Consider the following Rust code that uses tuple destructuring:

let tuple = (10, 20, 30);
let (a, b, c) = tuple;
println!("{} {} {}", a, b, c);

What will be printed?

Rust
let tuple = (10, 20, 30);
let (a, b, c) = tuple;
println!("{} {} {}", a, b, c);
A10 30 20
B30 20 10
C10 20 30
DCompilation error due to mismatched types
Attempts:
2 left
💡 Hint

Remember that tuple destructuring assigns values in order.

❓ Predict Output
intermediate
2:00remaining
What does this struct destructuring print?

Given this Rust code with a struct and destructuring:

struct Point { x: i32, y: i32 }

let p = Point { x: 5, y: 10 };
let Point { x: a, y: b } = p;
println!("{} {}", a, b);

What is the output?

Rust
struct Point { x: i32, y: i32 }

let p = Point { x: 5, y: 10 };
let Point { x: a, y: b } = p;
println!("{} {}", a, b);
A5 10
B10 5
CCompilation error: cannot destructure struct
D0 0
Attempts:
2 left
💡 Hint

Destructuring assigns fields to new variable names.

❓ Predict Output
advanced
2:00remaining
What is printed by this nested destructuring?

Analyze this Rust code with nested destructuring:

let data = (Some(3), Ok(7));
let (Some(x), Ok(y)) = data;
println!("{} {}", x, y);

What will be printed?

Rust
let data = (Some(3), Ok(7));
let (Some(x), Ok(y)) = data;
println!("{} {}", x, y);
A3 7
BSome(3) Ok(7)
CCompilation error: cannot destructure enums this way
DRuntime panic due to pattern mismatch
Attempts:
2 left
💡 Hint

Remember that pattern matching can destructure enum variants.

❓ Predict Output
advanced
2:00remaining
What error does this destructuring cause?

Consider this Rust code snippet:

let tuple = (1, 2);
let (a, b, c) = tuple;
println!("{} {} {}", a, b, c);

What error will this code produce?

Rust
let tuple = (1, 2);
let (a, b, c) = tuple;
println!("{} {} {}", a, b, c);
Aerror: variable c is unused
Berror: mismatched tuple size: expected a 3-element tuple but found a 2-element tuple
Cprints: 1 2 0
Dcompiles and prints: 1 2 3
Attempts:
2 left
💡 Hint

Check the number of elements in the tuple and pattern.

🧠 Conceptual
expert
2:00remaining
How many variables are bound in this pattern?

Given this Rust pattern:

let (a, (b, c), d) = (1, (2, 3), 4);

How many variables are bound after this destructuring?

Rust
let (a, (b, c), d) = (1, (2, 3), 4);
A3
B5
C2
D4
Attempts:
2 left
💡 Hint

Count each variable name on the left side of the assignment.

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

  1. Step 1: Understand tuple structure

    A tuple groups multiple values together as one compound value.
  2. Step 2: Apply destructuring

    Destructuring lets you assign each element of the tuple to its own variable easily.
  3. Final Answer:

    Break the tuple into individual variables easily -> Option C
  4. 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

  1. Step 1: Recall tuple destructuring syntax

    Rust uses parentheses to destructure tuples, matching the tuple structure.
  2. Step 2: Match variables to tuple elements

    Using let (a, b) = (x, y); correctly assigns a to x and b to y.
  3. Final Answer:

    let (a, b) = (x, y); -> Option A
  4. 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

  1. Step 1: Destructure tuple values

    The tuple point has values (3, 7). Destructuring assigns x = 3 and y = 7.
  2. Step 2: Print the variables

    The println! macro outputs the values of x and y in order.
  3. Final Answer:

    x = 3, y = 7 -> Option A
  4. 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

  1. Step 1: Check tuple length vs variables

    The tuple has 3 values but only 2 variables are provided for destructuring.
  2. Step 2: Understand destructuring rules

    Rust requires the number of variables to match the tuple elements exactly.
  3. Final Answer:

    Too many values to destructure into two variables -> Option D
  4. 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?
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,
};
hard
A. 37.68
B. 12.0
C. 7.0
D. Compilation error

Solution

  1. Step 1: Identify the variant matched

    The variable shape is a Rectangle with width 3.0 and height 4.0.
  2. Step 2: Calculate area using destructured values

    The match arm for Rectangle multiplies width and height: 3.0 * 4.0 = 12.0.
  3. Final Answer:

    12.0 -> Option B
  4. Quick Check:

    Rectangle area = width * height = 12.0 [OK]
Hint: Match enum variant and destructure fields to compute result [OK]
Common Mistakes:
  • Confusing Circle and Rectangle variants
  • Using radius formula for Rectangle
  • Forgetting to destructure fields properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard