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
- Rust parallel loop example
- How to iterate linked list in Rust
- How to iterate string lines in Rust
- How to iterate through hashmap keys in Rust
- How to iterate hashset in Rust
- How to do a for loop with index in Rust
- How to iterate directory in Rust
- How to iterate btreemap in Rust
- How to sleep in a loop in Rust
More of Rust
- How to match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to split a string with Rust regex?
- How to iterate over a Rust slice with an index?
- How to use negation in Rust regex?
- How to use regex captures in Rust?
- Regex example to match multiline string in Rust?
- How to get a capture group using Rust regex?
- How to use modifiers in a Rust regex?
- How to create a HashMap of structs in Rust?
See more codes...