← Back to Rust Course | Chapter 8: Enums & Pattern Matching | Lesson 7 of 7

Destructuring

Destructuring is unpacking a value into separate named pieces all at once, like opening a gift box and taking out each item.
Syntax
rust
let (a, b) = tuple;
let Name { field1, field2 } = value;
let [first, .., last] = array;

Destructuring a Tuple

A let statement can destructure a tuple directly into multiple named variables in one line, instead of accessing each element with .0, .1, and so on.

Example: Destructuring a Tuple

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `point`, set to `(3, 7)`
    let point = (3, 7);
    // Destructure `point` into `x, y`
    let (x, y) = point;
    // Print "x={}, y={}" to the console, substituting in `x, y` (with a trailing newline)
    println!("x={}, y={}", x, y);
}

Destructuring a Struct

Struct fields can be destructured into local variables of the same names directly in a let statement, using the struct's field names as the pattern.

Example: Destructuring a Struct

markup
// Define a struct `Point` to group related fields together
struct Point {
    x: i32,
    y: i32,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `p`, set to `Point { x: 5, y: 9 }`
    let p = Point { x: 5, y: 9 };
    let Point { x, y } = p;
    // Print "x={}, y={}" to the console, substituting in `x, y` (with a trailing newline)
    println!("x={}, y={}", x, y);
}

Ignoring Fields with ..

When you only need a few fields from a larger struct, .. in the pattern tells Rust to ignore all remaining fields without naming them individually.

Example: Ignoring Fields with ..

markup
// Define a struct `Config` to group related fields together
struct Config {
    width: u32,
    height: u32,
    fullscreen: bool,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `cfg`, set to `Config { width: 1920, height: 1080, fullscreen: true }`
    let cfg = Config { width: 1920, height: 1080, fullscreen: true };
    let Config { width, .. } = cfg;
    // Print "Width: {}" to the console, substituting in `width` (with a trailing newline)
    println!("Width: {}", width);
}

Discarding Values with _

The underscore pattern _ matches any value while discarding it, useful in destructuring or function parameters when a value must be present but is not actually needed.

Example: Discarding Values with _

markup
// Entry point: execution of the program starts here
fn main() {
    // Destructure `(1, 2, 3)` into `first, _, third`
    let (first, _, third) = (1, 2, 3);
    // Print "first={}, third={}" to the console, substituting in `first, third` (with a trailing newline)
    println!("first={}, third={}", first, third);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to destructure only some fields of a struct without using .. to explicitly ignore the rest, which is a compile error.
  2. Forgetting the _ pattern can be used to intentionally discard a value you don't need during destructuring.
  3. Assuming destructuring a reference automatically dereferences everything -- sometimes an explicit & pattern or .clone() is needed.
Chapter Summary
  • Destructuring extracts multiple named values from a tuple, struct, or enum in a single let or match.
  • The .. pattern can be used to ignore remaining fields you don't care about.
  • The _ pattern matches and discards any single value without binding it to a name.
  • Destructuring works in let statements, function parameters, and match arms alike.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.