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

if let का Syntax

if let किसी value के सिर्फ एक interesting case को handle करने के लिए एक shortcut है, हर possibility लिखे बिना।
Syntax
rust
if let Pattern(binding) = value {
    // body
} else {
    // no match
}

Basic if let

if let एक value की तुलना एक single pattern से करता है और इसका block सिर्फ तब चलाता है जब वह pattern match करे, अन्यथा block को पूरी तरह skip करते हुए। यह एक match का shorthand है जो सिर्फ एक case की परवाह करता है।

उदाहरण: Basic if let

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `config_max` (a `Option<u8>`), set to `Some(3)`
    let config_max: Option<u8> = Some(3);
    // If `config_max` matches `Some(max)`, bind and run this block
    if let Some(max) = config_max {
        // Print "Maximum is configured to be {}" to the console, substituting in `max` (with a trailing newline)
        println!("Maximum is configured to be {}", max);
    }
}

if let With else

if let में एक else block जोड़ना आपको non-matching case भी handle करने देता है, जब सिर्फ एक interesting variant हो तो match से कम boilerplate के साथ पूरी coverage देते हुए।

उदाहरण: if let With else

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `value` (a `Option<i32>`), set to `None`
    let value: Option<i32> = None;
    // If `value` matches `Some(v)`, bind and run this block
    if let Some(v) = value {
        // Print "Got value: {}" to the console, substituting in `v` (with a trailing newline)
        println!("Got value: {}", v);
    // Otherwise, run this branch
    } else {
        // Print "No value present" to the console (with a trailing newline)
        println!("No value present");
    }
}

if let With an Enum

if let किसी भी enum के साथ काम करता है, सिर्फ Option के साथ नहीं, आपको बाकी को नज़रअंदाज़ करते हुए interest के एक single variant को concisely handle करने देते हुए।

उदाहरण: if let With an Enum

markup
// Define an enum `Shape` listing its possible variants
enum Shape {
    Circle(f64),
    Square(f64),
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `shape`, set to `Shape::Circle(3.0)`
    let shape = Shape::Circle(3.0);
    // If `shape` matches `Shape::Circle(radius)`, bind and run this block
    if let Shape::Circle(radius) = shape {
        // Print "Circle with radius {}" to the console, substituting in `radius` (with a trailing newline)
        println!("Circle with radius {}", radius);
    }
}

Choosing if let vs match

if let तब सबसे अच्छा है जब आप सिर्फ एक specific pattern की परवाह करें और बाकी सब concisely नज़रअंदाज़ करना चाहें; match तब बेहतर है जब आपको हर possible case explicitly handle करना हो।

उदाहरण: Choosing if let vs match

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `items`, set to `[Some(1), None, Some(3)]`
    let items = [Some(1), None, Some(3)];
    // Loop over `items.iter()`, binding each item to `item`
    for item in items.iter() {
        // If `item` matches `Some(n)`, bind and run this block
        if let Some(n) = item {
            // Print "Found: {}" to the console, substituting in `n` (with a trailing newline)
            println!("Found: {}", n);
        }
    }
}
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. एक wildcard _ => {} arm के साथ एक पूरा match उपयोग करना जब एक कहीं छोटा if let वही logic express कर देता।
  2. यह भूल जाना कि if let design से non-exhaustive है -- यह जानबूझकर हर उस case को नज़रअंदाज़ करता है जो दिए pattern से match नहीं करता।
  3. यह न समझना कि if let ... else non-matching case भी handle कर सकता है, concise syntax को पूरी coverage के साथ combine करते हुए।
चैप्टर सारांश
  • if let pattern = value { ... } block सिर्फ तब चलाता है जब value दिए pattern से match हो।
  • if let तब उपयोगी है जब किसी match का सिर्फ एक variant असल में मायने रखता हो, बाकी के लिए boilerplate avoid करते हुए।
  • if let से एक else branch attach किया जा सकता है ताकि बाकी हर case को एक जगह handle किया जाए।
  • match के विपरीत, if let को exhaustive होने की ज़रूरत नहीं।
🔒

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.