rustRust named loop example
A named loop in Rust is a loop that can be labeled with a name. This allows the loop to be breaked or continueed from anywhere in the code.
Example code
'outer: loop {
println!("Entered the outer loop");
'inner: loop {
println!("Entered the inner loop");
// This would break only the inner loop
break 'inner;
println!("This point will never be reached");
}
println!("This point is reached");
// This breaks the outer loop
break 'outer;
}
println!("Exited the outer loop");
Output example
Entered the outer loop
Entered the inner loop
This point is reached
Exited the outer loop
Code explanation
'outer: loop- This is the outer loop, labeled with the nameouter.break 'inner- This statement breaks the inner loop, labeled with the nameinner.break 'outer- This statement breaks the outer loop, labeled with the nameouter.
Helpful links
Related
- How to loop until error in Rust
- How to iterate hashset in Rust
- How to iterate lines in file in Rust
- How to sleep in a loop in Rust
- How to iterate btreemap in Rust
- Rust for loop range inclusive example
- How to iterate linked list in Rust
- How to iterate through hashmap keys in Rust
- How to iterate in pairs in Rust
More of Rust
- How to replace a capture group using Rust regex?
- Weak pointer example in Rust
- How to use regex lookbehind in Rust?
- How to perform matrix operations in Rust?
- How to split a string with Rust regex?
- How to match whitespace with a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to use regex captures in Rust?
- How to use non-capturing groups in Rust regex?
- Regex example to match multiline string in Rust?
See more codes...