RRust By Example

Tuple Destructuring

Create and destructure tuples in Rust.

Tuple Destructuring

Create and destructure tuples in Rust.

Difficulty

Beginner

Code

rust
fn main() {
    let point = (10, 20, 30);
    let (x, y, z) = point;
    println!("x={}, y={}, z={}", x, y, z);

    let (first, ..) = point;
    println!("first: {}", first);

    println!("access by index: {}", point.1);
}

Explanation

This example demonstrates how to use tuple destructuring in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.

Key Concepts

  • Rust's strong type system catches errors at compile time
  • Ownership and borrowing rules ensure memory safety
  • Pattern matching makes code expressive and exhaustive

Related Topics

Browse more examples in the compound-types category to build a complete understanding of this topic.

More Compound Types Examples