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 do a for loop with index in Rust
- How to iterate string lines in Rust
- Rust parallel loop example
- How to iterate over ndarray rows in Rust
- How to iterate btreemap in Rust
- Rust for loop range inclusive example
- Rust negative for loop example
- How to sleep in a loop in Rust
More of Rust
- How to match a URL with a regex in Rust?
- How to use non-capturing groups in Rust regex?
- How to use regex lookahead in Rust?
- How to match the end of a line in a Rust regex?
- How to replace a capture group using Rust regex?
- How to ignore case in Rust regex?
- How to replace strings using Rust regex?
- How to use negation in Rust regex?
- Regex example to match multiline string in Rust?
- How to use regex to match a group in Rust?
See more codes...