0Pricing
Learn Rust Coding · Lesson

Destructuring

Structs, enums, tuples.

Destructuring is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Destructuring?

Destructuring breaks a compound value apart into its pieces using a pattern. It works with tuples, structs, enums, arrays, and more.

Destructuring Tuples

Bind each element of a tuple to its own variable in a single let.

fn main() {
    let point = (3, 7);
    let (x, y) = point;
    println!("x = {x}, y = {y}");
}

Ignoring Tuple Parts

Use _ to skip a value and .. to skip several.

fn main() {
    let data = (1, 2, 3, 4);
    let (first, .., last) = data;
    println!("{first} ... {last}");
}

Destructuring Structs

Pull fields out of a struct by name. The bound variable names can match the fields or be renamed.

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

fn main() {
    let p = Point { x: 10, y: 20 };
    let Point { x, y } = p;
    println!("{x}, {y}");
}

Renaming Fields

Use field: new_name to bind a struct field to a different variable name.

struct Config {
    width: u32,
    height: u32,
}

fn main() {
    let c = Config { width: 800, height: 600 };
    let Config { width: w, height: h } = c;
    println!("{w} x {h}");
}

Ignoring Struct Fields

.. ignores the remaining fields you don't need.

struct User {
    name: String,
    age: u32,
    active: bool,
}

fn main() {
    let u = User { name: "Ada".into(), age: 36, active: true };
    let User { name, .. } = u;
    println!("{name}");
}

Destructuring Enums

In a match, destructure enum variants to extract their data.

enum Event {
    Click { x: i32, y: i32 },
    Key(char),
}

fn main() {
    let e = Event::Click { x: 5, y: 8 };
    match e {
        Event::Click { x, y } => println!("click at {x},{y}"),
        Event::Key(c) => println!("key {c}"),
    }
}

Destructuring in Parameters

Function parameters can destructure directly, which is handy for tuples and small structs.

fn distance((x1, y1): (f64, f64), (x2, y2): (f64, f64)) -> f64 {
    ((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt()
}

fn main() {
    println!("{}", distance((0.0, 0.0), (3.0, 4.0)));
}

Nested Destructuring

Patterns nest freely, so you can reach deep into structures in one step.

struct Line {
    start: (i32, i32),
    end: (i32, i32),
}

fn main() {
    let line = Line { start: (0, 0), end: (4, 5) };
    let Line { start: (sx, sy), end: (ex, ey) } = line;
    println!("from {sx},{sy} to {ex},{ey}");
}

Destructuring Arrays and Slices

Arrays and slices can be destructured, including capturing the middle with ...

fn main() {
    let arr = [1, 2, 3, 4, 5];
    let [first, .., last] = arr;
    println!("first {first}, last {last}");
}

Destructuring in for Loops

Destructuring works in for loops too, which is perfect for iterating over pairs of values.

fn main() {
    let pairs = [("a", 1), ("b", 2), ("c", 3)];
    for (label, value) in pairs {
        println!("{label} = {value}");
    }
}

Quick Check

How do you ignore the remaining fields of a struct while destructuring?

Recap

You learned destructuring:

  • Break apart tuples, structs, enums, arrays, and slices
  • Rename fields with field: name
  • Ignore parts with _ and ..
  • Destructure in let, match, and function parameters
  • Patterns nest for deep extraction

Frequently asked questions

Is the “Destructuring” lesson free?

Yes — the full text of “Destructuring” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Destructuring”?

Structs, enums, tuples. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Destructuring” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. match Deep Dive
  2. if let and while let
  3. Binding with @
  4. Destructuring
← Back to Learn Rust Coding