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

Nested Patterns क्या हैं

Nested patterns आपको दूसरी values से बनी किसी value के अंदर गहराई से देखने देते हैं, सब एक single match arm में।
Syntax
rust
match value {
    Outer::Variant(Inner::Variant(x)) => expression,
    Outer::Variant(_) => expression,
    _ => expression,
}

Matching a Struct Inside an Enum

एक pattern सीधे एक struct तक पहुंच सकता है जो एक enum variant के अंदर wrapped है, उसी match arm में इसकी fields निकालते हुए।

उदाहरण: Matching a Struct Inside an Enum

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

// Define an enum `Shape` listing its possible variants
enum Shape {
    Dot(Point),
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `shape`, set to `Shape::Dot(Point { x: 3, y: 4 })`
    let shape = Shape::Dot(Point { x: 3, y: 4 });
    // Match on `shape` and branch by pattern
    match shape {
        Shape::Dot(Point { x, y }) => println!("Dot at ({}, {})", x, y),
    }
}

Matching Nested Tuples

Tuples एक-दूसरे के अंदर nested हो सकते हैं, और एक single pattern एक साथ सभी levels destructure कर सकता है, आपको चाहे जिस inner values से names bind करते हुए।

उदाहरण: Matching Nested Tuples

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

Matching an Option Inside an Enum Variant

Patterns wrapping की कई layers के through drill कर सकते हैं, जैसे किसी enum variant के अंदर stored एक Option, सीधे innermost value निकालते हुए।

उदाहरण: Matching an Option Inside an Enum Variant

markup
// Define an enum `Response` listing its possible variants
enum Response {
    Data(Option<i32>),
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `resp`, set to `Response::Data(Some(99))`
    let resp = Response::Data(Some(99));
    // Match on `resp` and branch by pattern
    match resp {
        Response::Data(Some(value)) => println!("Got data: {}", value),
        Response::Data(None) => println!("No data"),
    }
}

Keeping Nesting Readable

हालांकि Rust arbitrarily deep nested patterns allow करता है, इन्हें एक नज़र में पढ़ने लायक shallow रखना एक अच्छा practice है, ज़रूरत पड़ने पर बहुत deep structures को छोटे matches या helper functions में split करते हुए।

उदाहरण: Keeping Nesting Readable

markup
// Define a struct `Address` to group related fields together
struct Address {
    city: String,
}

// Define a struct `Person` to group related fields together
struct Person {
    address: Address,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `person`, set to `Person { address: Address { city: String::from("Kyoto") } }`
    let person = Person { address: Address { city: String::from("Kyoto") } };
    let Person { address: Address { city } } = person;
    // Print "City: {}" to the console, substituting in `city` (with a trailing newline)
    println!("City: {}", city);
}
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. #}
आम गलतियां
  1. inner pattern को ठीक से wrap किए बिना nested enum variants match करने की कोशिश करना, एक syntax error का कारण बनते हुए।
  2. अत्यधिक deep nested patterns लिखना जो पढ़ने में मुश्किल हो जाते हैं, जब एक helper function या कई matches में split करना ज़्यादा clear होता।
  3. यह भूल जाना कि एक nested Option<Option<T>> या similar double-wrapped structure को पूरी तरह unwrap करने के लिए pattern matching की दो layers चाहिए।
चैप्टर सारांश
  • Patterns एक-दूसरे के अंदर nest हो सकते हैं, एक single arm में Some(Point { x, y }) जैसी structures match करते हुए।
  • Nested patterns enums, structs, और tuples को एक match expression में combine कर सकते हैं।
  • Deeply nested data को सीधे destructure किया जा सकता है, अलग match या if-let statements की एक chain से बचते हुए।
  • अत्यधिक deep nesting readability को नुकसान पहुंचा सकता है, इसलिए patterns को reasonably shallow रखना एक अच्छा practice है।
🔒

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.