位置:首页 > > Rust嵌套和标签

Rust嵌套和标签

外循环可以使用break或continue,当在处理嵌套循环时。在这种情况, 循环必须用一些被注解:label, 标签必须被传递给 break/continue语句.
#![allow(unreachable_code)]

fn main() {
    'outer: loop {
        println!("Entered the outer loop");

        'inner: loop {
            println!("Entered the inner loop");

            // This would break only the inner loop
            //break;

            // This breaks the outer loop
            break 'outer;
        }

        println!("This point will never be reached");
    }

    println!("Exited the outer loop");
}