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 iterate hashset in Rust
- How to loop until error in Rust
- How to iterate linked list in Rust
- How to sleep in a loop in Rust
- How to do a for loop with index in Rust
- Rust for loop range inclusive example
- How to iterate in pairs in Rust
- Rust parallel loop example
- How to iterate over string in Rust
- Rust negative for loop example
More of Rust
- How to use regex to match a group in Rust?
- How to use regex to match a double quote in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to use regex with bytes in Rust?
- How to parse JSON string in Rust?
- How to match the end of a line in a Rust regex?
- How to match whitespace with a regex in Rust?
- How to split a string by regex in Rust?
- Regex example to match multiline string in Rust?
- Hashshet example in Rust
See more codes...