Back to questions
JuniorTheoryOccasional
What does the program print?
fn main() {
let mut passes = 0;
'outer: for i in 0..3 {
for j in 0..3 {
if i + j == 2 {
break 'outer;
}
passes += 1;
}
}
println!("{passes}");
}2
- Expecting
3, as with a plainbreak: a barebreakleaves only the nearest loop, whilebreak 'outerends the labelled outer loop too. - Expecting
6, mixing upbreakandcontinue:continueskips one pass, whilebreak 'outerstops both loops completely.
- What would the program print with
continue 'outerinstead ofbreak 'outer? - Why may
break 'outernot carry a value when'outerlabels aforloop?