Match Arms को गहराई से समझना
In this page:
match value {
Enum::Variant1 => expression,
Enum::Variant2(x) if condition => expression,
Enum::Variant3 { field } => expression,
_ => expression,
}
Binding Values in Match Arms
एक match arm का pattern matched value के हिस्से को एक नए variable से bind कर सकता है, उस data को सिर्फ उस specific arm के code के अंदर available बनाते हुए।
उदाहरण: Binding Values in Match Arms
// Define an enum `Message` listing its possible variants
enum Message {
Text(String),
Number(i32),
}
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `msg`, set to `Message::Number(42)`
let msg = Message::Number(42);
// Match on `msg` and branch by pattern
match msg {
Message::Text(s) => println!("Text: {}", s),
Message::Number(n) => println!("Number: {}", n),
}
}
Login to try C/C++/Java/PHP code in the editor
Matching Ranges
एक pattern start..=end उपयोग करके values की एक inclusive range match कर सकता है, जो हर individual value list करने या एक अलग if chain लिखने से ज़्यादा concise है।
उदाहरण: Matching Ranges
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `score`, set to `82`
let score = 82;
// Match on `score` and branch by pattern
match score {
90..=100 => println!("A"),
80..=89 => println!("B"),
70..=79 => println!("C"),
_ => println!("Below C"),
}
}
Login to try C/C++/Java/PHP code in the editor
Match Guards
एक match guard किसी pattern के बाद एक extra if condition जोड़ता है, एक arm को सिर्फ तब match करने देते हुए जब pattern और extra condition दोनों true हों।
उदाहरण: Match Guards
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `pair`, set to `(4, -4)`
let pair = (4, -4);
// Match on `pair` and branch by pattern
match pair {
(x, y) if x == -y => println!("These are opposites"),
(x, y) if x == y => println!("These are equal"),
_ => println!("No special relationship"),
}
}
Login to try C/C++/Java/PHP code in the editor
Combining Multiple Patterns in One Arm
| operator कई patterns, ranges सहित, को एक single arm में combine कर सकता है ताकि related cases एक code block share करें।
उदाहरण: Combining Multiple Patterns in One Arm
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `ch`, set to `'7'`
let ch = '7';
// Match on `ch` and branch by pattern
match ch {
'0'..='9' => println!("It is a digit"),
'a'..='z' | 'A'..='Z' => println!("It is a letter"),
_ => println!("Something else"),
}
}
Login to try C/C++/Java/PHP code in the editor
- Match arms इस तरह order करना कि एक broad pattern (जैसे एक range) गलती से बाद में listed किसी ज़्यादा specific एक को shadow कर दे।
- यह भूल जाना कि एक match guard (
if condition) किसी pattern पर finer-grained control के लिए extra conditions जोड़ सकता है। - यह न समझना कि किसी match arm में bound variables, जैसे
Some(n) =>मेंn, सिर्फ उस specific arm के अंदर valid हैं।
- हर match arm एक pattern को उस code के साथ जोड़ता है जो उस pattern के match होने पर चलता है।
1..=5जैसे Range patterns एक inclusive range के अंदर किसी भी value को match करते हैं।- Match guards किसी pattern पर ज़्यादा precise matching के लिए एक extra
if conditionजोड़ते हैं। - किसी pattern से introduce किए गए bound variables (जैसे किसी enum से data निकालना) उनके arm तक scoped होते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: