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

for Loop और Range

एक for loop आपको numbers या items की एक list में एक-एक करके, अपने आप, बिना किसी bookkeeping के चलने देता है।
Syntax
rust
for item in collection {
    // body
}

for i in start..end {
    // end is exclusive
}

for i in start..=end {
    // end is inclusive
}

Iterating a Range

0..5 जैसी range sequence 0, 1, 2, 3, 4 produce करती है -- note करें upper bound exclusive है। एक for loop किसी manual counter के बिना सीधे ऐसी range पर iterate कर सकता है।

उदाहरण: Iterating a Range

markup
// Entry point: execution of the program starts here
fn main() {
    // Loop over `0..5`, binding each item to `i`
    for i in 0..5 {
        // Print "i = {}" to the console, substituting in `i` (with a trailing newline)
        println!("i = {}", i);
    }
}

Inclusive Ranges

एक equals sign जोड़ना, 0..=5, एक inclusive range बनाता है जिसमें final number शामिल है, 0 से 5 तक produce करते हुए। इसे उपयोग करें जब आप endpoint को शामिल चाहते हों।

उदाहरण: Inclusive Ranges

markup
// Entry point: execution of the program starts here
fn main() {
    // Loop over `1..=3`, binding each item to `i`
    for i in 1..=3 {
        // Print "Count: {}" to the console, substituting in `i` (with a trailing newline)
        println!("Count: {}", i);
    }
}

Iterating a Collection

for loops सीधे किसी array या अन्य collection के elements पर iterate कर सकते हैं, आपको हर value बारी-बारी से देते हुए बिना कभी किसी index को छुए।

उदाहरण: Iterating a Collection

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `fruits`, set to `["apple", "banana", "cherry"]`
    let fruits = ["apple", "banana", "cherry"];
    // Loop over `fruits.iter()`, binding each item to `fruit`
    for fruit in fruits.iter() {
        // Print "Fruit: {}" to the console, substituting in `fruit` (with a trailing newline)
        println!("Fruit: {}", fruit);
    }
}

Using enumerate for Index and Value

जब आपको iterating करते समय position और value दोनों चाहिए, .enumerate() एक iterator को wrap करता है ताकि (index, value) pairs yield करे, manual counters को पूरी तरह avoid करते हुए।

उदाहरण: Using enumerate for Index and Value

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `colors`, set to `["red", "green", "blue"]`
    let colors = ["red", "green", "blue"];
    // Loop over `colors.iter().enumerate()`, binding each item to `(index, color)`
    for (index, color) in colors.iter().enumerate() {
        // Print "{}: {}" to the console, substituting in `index, color` (with a trailing newline)
        println!("{}: {}", index, color);
    }
}
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. for i in 0..n लिखना जब आपको असल में n शामिल करना हो, यह भूल जाना कि .. upper bound को exclude करता है।
  2. Rust के safer और ज़्यादा idiomatic for loop उपयोग करने के बजाय हाथ से एक index variable और while loop manage करना।
  3. किसी collection को सीधे value से iterate करते समय इसकी length modify करने की कोशिश करना, जिसे borrow checker reject कर देगा।
चैप्टर सारांश
  • for item in collection किसी collection या range के हर element पर अपने आप iterate करता है।
  • 0..5 एक exclusive range है (0 से 4); 0..=5 inclusive है (0 से 5)।
  • for loops को manual indexing से ज़्यादा पसंद किया जाता है क्योंकि वे off-by-one errors खत्म करते हैं।
  • .enumerate() को for के साथ combine किया जा सकता है ताकि index और value दोनों मिलें।
🔒

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.