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

loop Keyword का उपयोग

loop आपके program को किसी चीज़ को हमेशा के लिए दोहराने देता है, जब तक आप इसे रुकने को न कहें।
Syntax
rust
loop {
    // body
    if condition {
        break;
    }
}

A Basic Infinite Loop

loop keyword अपने block को हमेशा के लिए दोहराता है जब तक एक break statement explicitly इससे बाहर न निकले। यह तब उपयोगी है जब आपको पहले से पता न हो कि कितनी iterations चाहिए।

उदाहरण: A Basic Infinite Loop

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `count`, initialized to `0`
    let mut count = 0;
    // Loop forever until an explicit `break`
    loop {
        count += 1;
        // Print "count = {}" to the console, substituting in `count` (with a trailing newline)
        println!("count = {}", count);
        // Check whether `count == 3`
        if count == 3 {
            break;
        }
    }
}

Returning a Value from loop

ज़्यादातर looping constructs के विपरीत, loop एक expression है: आप break को एक value पास कर सकते हैं, और वह पूरे loop expression की value बन जाती है जिसे यह evaluate करता है।

उदाहरण: Returning a Value from loop

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `counter`, initialized to `0`
    let mut counter = 0;
    // Declare an immutable variable `result`; its value comes from the block below
    let result = loop {
        counter += 1;
        // Check whether `counter == 5`
        if counter == 5 {
            break counter * 2;
        }
    };
    // Print "result = {}" to the console, substituting in `result` (with a trailing newline)
    println!("result = {}", result);
}

Labeled Loops

जब loops nested होते हैं, आप outer वाले को 'outer: जैसे नाम से label कर सकते हैं और उस label को break या continue के साथ उपयोग करके सीधे inner वाले के अंदर से outer loop control कर सकते हैं।

उदाहरण: Labeled Loops

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `total`, initialized to `0`
    let mut total = 0;
    'outer: loop {
        // Declare a mutable variable `inner_count`, initialized to `0`
        let mut inner_count = 0;
        // Loop forever until an explicit `break`
        loop {
            total += 1;
            inner_count += 1;
            // Check whether `inner_count == 2`
            if inner_count == 2 {
                break;
            }
            // Check whether `total >= 6`
            if total >= 6 {
                break 'outer;
            }
        }
        // Check whether `total >= 6`
        if total >= 6 {
            break;
        }
    }
    // Print "total = {}" to the console, substituting in `total` (with a trailing newline)
    println!("total = {}", total);
}

loop for Retry Logic

loop का एक आम real-world उपयोग किसी operation को तब तक retry करना है जब तक यह succeed न हो, सिर्फ top पर नहीं बल्कि body के अंदर कहीं एक condition जांचते हुए।

उदाहरण: loop for Retry Logic

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare a mutable variable `attempts`, initialized to `0`
    let mut attempts = 0;
    // Declare an immutable variable `success`; its value comes from the block below
    let success = loop {
        attempts += 1;
        // Check whether `attempts == 3`
        if attempts == 3 {
            break true;
        }
    };
    // Print "Succeeded after {} attempts: {}" to the console, substituting in `attempts, success` (with a trailing newline)
    println!("Succeeded after {} attempts: {}", attempts, success);
}
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. एक infinite loop लिखना और एक break condition भूल जाना, program को हमेशा के लिए hang करने का कारण बनते हुए।
  2. यह न समझना कि loop break value; के ज़रिए एक value return कर सकता है, और इसके बजाय manually एक result variable track करना।
  3. loop (हमेशा दोहराता है) को while (सिर्फ एक condition true रहने तक दोहराता है) के साथ confuse करना।
चैप्टर सारांश
  • loop एक code block को indefinitely दोहराता है जब तक एक break statement reach न हो।
  • break value; एक loop से बाहर निकल सकता है साथ ही एक value भी produce कर सकता है जिस तक loop expression evaluate होता है।
  • Loop labels ('outer: loop { ... }) break/continue को एक specific outer loop target करने देते हैं।
  • loop अक्सर retry logic के लिए या जब exit condition body के बीच में जांचा जाता है तब उपयोग होता है।
🔒

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.