Value के साथ Break करना
break को result handover करा सकते हैं, ताकि loop खुद एक सवाल का answer बन जाए।In this page:
let name = loop {
// body
if condition {
break value;
}
};
Returning a Value on Break
जब एक loop को break some_value; से बाहर निकाला जाता है, वह value पूरे loop expression का result बन जाती है, जिसे फिर एक variable में store किया जा सकता है।
उदाहरण: Returning a Value on Break
// 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 == 10`
if counter == 10 {
break counter;
}
};
// Print "Loop returned: {}" to the console, substituting in `result` (with a trailing newline)
println!("Loop returned: {}", result);
}
Login to try C/C++/Java/PHP code in the editor
Multiple break Points
एक single loop में अलग-अलग code paths पर एक से ज़्यादा break statements हो सकते हैं, जब तक उनमें से हर एक एक जैसे type की value produce करे।
उदाहरण: Multiple break Points
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `n`, initialized to `1`
let mut n = 1;
// Declare an immutable variable `outcome`; its value comes from the block below
let outcome = loop {
n *= 2;
// Check whether `n > 100`
if n > 100 {
break "too big";
}
// Check whether `n == 64`
if n == 64 {
break "found sixty-four";
}
};
// Print "{}" to the console, substituting in `outcome` (with a trailing newline)
println!("{}", outcome);
}
Login to try C/C++/Java/PHP code in the editor
Why Only loop Supports This
while और for loops भी इसलिए खत्म हो सकते हैं क्योंकि उनका condition false हो गया या iterator खत्म हो गया, इसलिए return करने के लिए कोई single guaranteed exit value नहीं है। loop का कोई implicit exit नहीं है, इसलिए break value unambiguous है।
उदाहरण: Why Only loop Supports This
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `target`, set to `5`
let target = 5;
// Declare a mutable variable `current`, initialized to `0`
let mut current = 0;
// Declare an immutable variable `found_at`; its value comes from the block below
let found_at = loop {
current += 1;
// Check whether `current == target`
if current == target {
break current;
}
};
// Print "Found target at iteration {}" to the console, substituting in `found_at` (with a trailing newline)
println!("Found target at iteration {}", found_at);
}
Login to try C/C++/Java/PHP code in the editor
Combining with match on the Result
चूंकि break value एक ordinary value produce करता है, आप इसे तुरंत दूसरे expressions के साथ उपयोग कर सकते हैं, जैसे इसे सीधे एक match में feed करना।
उदाहरण: Combining with match on the Result
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `i`, initialized to `0`
let mut i = 0;
// Declare an immutable variable `parity`; its value comes from the block below
let parity = loop {
i += 3;
// Check whether `i > 10`
if i > 10 {
break i % 2;
}
};
// Match on `parity` and branch by pattern
match parity {
0 => println!("Ended on an even number"),
_ => println!("Ended on an odd number"),
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी
forयाwhileloop के अंदरbreak value;उपयोग करने की कोशिश करना -- सिर्फloopइस तरह एक value return कर सकता है। - किसी
loopexpression के result assign करते समय semicolon भूल जाना, क्योंकि पूरेloop {}को एक expression माना जाता है। - उसी
loopके अंदर अलग-अलगbreakstatements से produce होने वाले types को mismatch करना, जो compile होने में fail होता है।
break value;सिर्फloopके अंदर काम करता है, क्योंकि सिर्फloopकी गारंटी है कि यह सिर्फbreakके ज़रिए exit होता है।breakको पास की गई value पूरेloopexpression की value बन जाती है।- उसी
loopके अंदर सभीbreakstatements को एक जैसे type की values produce करनी चाहिए। - यह pattern retry-until-success logic के लिए आम है जहां result सिर्फ loop खत्म होने पर ही पता चलता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: