for Loop और Range
for loop आपको numbers या items की एक list में एक-एक करके, अपने आप, बिना किसी bookkeeping के चलने देता है।In this page:
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Inclusive Ranges
एक equals sign जोड़ना, 0..=5, एक inclusive range बनाता है जिसमें final number शामिल है, 0 से 5 तक produce करते हुए। इसे उपयोग करें जब आप endpoint को शामिल चाहते हों।
उदाहरण: Inclusive Ranges
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating a Collection
for loops सीधे किसी array या अन्य collection के elements पर iterate कर सकते हैं, आपको हर value बारी-बारी से देते हुए बिना कभी किसी index को छुए।
उदाहरण: Iterating a Collection
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
for i in 0..nलिखना जब आपको असल मेंnशामिल करना हो, यह भूल जाना कि..upper bound को exclude करता है।- Rust के safer और ज़्यादा idiomatic
forloop उपयोग करने के बजाय हाथ से एक index variable औरwhileloop manage करना। - किसी collection को सीधे value से iterate करते समय इसकी length modify करने की कोशिश करना, जिसे borrow checker reject कर देगा।
for item in collectionकिसी collection या range के हर element पर अपने आप iterate करता है।0..5एक exclusive range है (0 से 4);0..=5inclusive है (0 से 5)।forloops को 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: