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

Match Arms को गहराई से समझना

Match arms वे अलग-अलग answers हैं जो आप किसी value के हर possible shape के लिए लिखते हैं।
Syntax
rust
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

markup
// 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),
    }
}

Matching Ranges

एक pattern start..=end उपयोग करके values की एक inclusive range match कर सकता है, जो हर individual value list करने या एक अलग if chain लिखने से ज़्यादा concise है।

उदाहरण: Matching Ranges

markup
// 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"),
    }
}

Match Guards

एक match guard किसी pattern के बाद एक extra if condition जोड़ता है, एक arm को सिर्फ तब match करने देते हुए जब pattern और extra condition दोनों true हों।

उदाहरण: Match Guards

markup
// 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"),
    }
}

Combining Multiple Patterns in One Arm

| operator कई patterns, ranges सहित, को एक single arm में combine कर सकता है ताकि related cases एक code block share करें।

उदाहरण: Combining Multiple Patterns in One Arm

markup
// 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"),
    }
}
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. Match arms इस तरह order करना कि एक broad pattern (जैसे एक range) गलती से बाद में listed किसी ज़्यादा specific एक को shadow कर दे।
  2. यह भूल जाना कि एक match guard (if condition) किसी pattern पर finer-grained control के लिए extra conditions जोड़ सकता है।
  3. यह न समझना कि किसी 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:

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.