Syntax for returning in multiple loops

let a = loop {
    let b = loop {
        break a; // should return to a
        break b; // should return to b
    }
}

How to write this

You can do it like this:

fn main() {
    let mut i:usize = 0;
    'a: loop {
        dbg!("loop a");
        'b: loop {
            i += 1;
            dbg!("loop b");
            dbg!(i);
            if i > 10 { break 'a; }
            if i % 3 == 0 { break 'b; }
        }
        dbg!("exit b");
    }
    dbg!("exit a");
}
4 Likes

Not exactly the solution what I wanted, but I figured it out:

let a = 'a: loop {
    let b = 'b: loop {
        if /*condition */ { break 'a 0 } // goes to a
        if /*condition */ { break 'b 1 } // goes to b
    }
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.