Use a label (e.g., 'outer) before a loop to target it with break, and append a value to break to return it from the loop block.
fn main() {
let mut count = 0;
'counting_up: loop {
println!("count = {count}");
let mut remaining = 10;
loop {
println!("remaining = {remaining}");
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}
count += 1;
}
let result = loop {
count += 1;
if count == 10 {
break count * 2;
}
};
println!("The result is {result}");
}