patternrustCritical
Named breaks in for loops in Rust
Viewed 0 times
breaksforrustnamedloops
Problem
Is there a way to have nested
for loops in Rust and break the outer one from inside the inner one, the way one could do e.g. in Java? I know Rust supports named breaks in loop but I can't seem to find information about the same regarding for.Solution
Yes. It uses the same syntax as lifetimes.
See loop labels documentation and the related section of the reference.
fn main() {
'outer: for x in 0..5 {
'inner: for y in 0..5 {
println!("{},{}", x, y);
if y == 3 {
break 'outer;
}
}
}
}See loop labels documentation and the related section of the reference.
Code Snippets
fn main() {
'outer: for x in 0..5 {
'inner: for y in 0..5 {
println!("{},{}", x, y);
if y == 3 {
break 'outer;
}
}
}
}Context
Stack Overflow Q#22905752, score: 107
Revisions (0)
No revisions yet.