Named breaks in for loops in Rust

后端 未结 1 537
醉酒成梦
醉酒成梦 2020-12-28 12:12

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

1条回答
  •  有刺的猬
    2020-12-28 12:49

    Yes. It uses the same syntax as lifetimes.

    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.

    0 讨论(0)
提交回复
热议问题