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 break
ed or continue
ed 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 linked list in Rust
- How to iterate over string in Rust
- How to iterate a map in Rust
- Rust for loop range inclusive example
- How to do a for loop with index in Rust
- How to iterate in pairs in Rust
- How to loop N times in Rust
- Rust negative for loop example
- Rust parallel loop example
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...