Nested Patterns क्या हैं
In this page:
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
// 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),
}
}
Login to try C/C++/Java/PHP code in the editor
Matching Nested Tuples
Tuples एक-दूसरे के अंदर nested हो सकते हैं, और एक single pattern एक साथ सभी levels destructure कर सकता है, आपको चाहे जिस inner values से names bind करते हुए।
उदाहरण: Matching Nested Tuples
// 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);
}
Login to try C/C++/Java/PHP code in the editor
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
// 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"),
}
}
Login to try C/C++/Java/PHP code in the editor
Keeping Nesting Readable
हालांकि Rust arbitrarily deep nested patterns allow करता है, इन्हें एक नज़र में पढ़ने लायक shallow रखना एक अच्छा practice है, ज़रूरत पड़ने पर बहुत deep structures को छोटे matches या helper functions में split करते हुए।
उदाहरण: Keeping Nesting Readable
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- inner pattern को ठीक से wrap किए बिना nested enum variants match करने की कोशिश करना, एक syntax error का कारण बनते हुए।
- अत्यधिक deep nested patterns लिखना जो पढ़ने में मुश्किल हो जाते हैं, जब एक helper function या कई matches में split करना ज़्यादा clear होता।
- यह भूल जाना कि एक 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: