← Back to Rust Course | Chapter 3: Control Flow | Lesson 5 of 7

match से Pattern Matching

match आपके program को एक value की कई possible options के साथ तुलना करने और हर एक के लिए अलग code चलाने देता है।
Syntax
rust
match value {
    pattern1 => expression,
    pattern2 | pattern3 => expression,
    low..=high => expression,
    _ => default_expression,
}

Basic match

match एक value की तुलना patterns की एक list से order में करता है और पहले matching pattern के लिए code execute करता है। यह दो से ज़्यादा possibilities पर branch करने के लिए Rust का primary tool है।

उदाहरण: Basic match

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `day`, set to `3`
    let day = 3;
    // Match on `day` and branch by pattern
    match day {
        1 => println!("Monday"),
        2 => println!("Tuesday"),
        3 => println!("Wednesday"),
        _ => println!("Some other day"),
    }
}

Exhaustiveness

Rust compiler चाहता है कि हर match matched type की सभी possible values cover करे। _ wildcard pattern एक catch-all है जो explicitly listed न की गई किसी भी values के लिए यह requirement पूरा करता है।

उदाहरण: Exhaustiveness

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `letter_grade`, set to `'B'`
    let letter_grade = 'B';
    // Match on `letter_grade` and branch by pattern
    match letter_grade {
        'A' => println!("Excellent"),
        'B' => println!("Good"),
        'C' => println!("Average"),
        _ => println!("Needs improvement"),
    }
}

Matching Multiple Values

किसी pattern के अंदर | operator एक single match arm को एक साथ कई अलग values handle करने देता है, identical code वाले repeated arms से बचते हुए।

उदाहरण: Matching Multiple Values

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `n`, set to `4`
    let n = 4;
    // Match on `n` and branch by pattern
    match n {
        1 | 3 | 5 | 7 => println!("{} is odd (from list)", n),
        2 | 4 | 6 | 8 => println!("{} is even (from list)", n),
        _ => println!("Out of range"),
    }
}

match as an Expression

if की तरह, match एक expression है और सीधे एक value produce कर सकता है, जिसे आमतौर पर arms में logic duplicate करने के बजाय एक variable को assign किया जाता है।

उदाहरण: match as an Expression

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `number`, set to `6`
    let number = 6;
    // Declare an immutable variable `description`; its value comes from the block below
    let description = match number % 2 {
        0 => "even",
        _ => "odd",
    };
    // Print "{} is {}" to the console, substituting in `number, description` (with a trailing newline)
    println!("{} is {}", number, description);
}
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. यह भूल जाना कि Rust में match exhaustive होना चाहिए -- type की हर possible value cover होनी चाहिए, नहीं तो यह compile नहीं होगा।
  2. match में _ को बहुत जल्दी उपयोग करना, गलती से उन cases को पकड़ते हुए जिन्हें बाद में listed किसी ज़्यादा specific arm को handle करना चाहिए था।
  3. C के switch statement की तरह match arms के बीच fall through करने की कोशिश करना -- Rust के match arms कभी fall through नहीं करते।
चैप्टर सारांश
  • match एक value की तुलना patterns की एक series से करता है और पहले matching वाले के लिए code चलाता है।
  • Matches exhaustive होने चाहिए; _ wildcard pattern बचे हुए किसी भी cases को पकड़ता है।
  • कई values एक arm share कर सकती हैं | pattern उपयोग करके, जैसे 1 | 2 => ...।
  • match एक expression है, इसलिए यह if की तरह एक value produce कर सकता है।
🔒

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.