Named breaks in for loops in Rust

青春壹個敷衍的年華 提交于 2019-11-30 13:37:14

问题


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 breaks in loop but I can't seem to find information about the same regarding for.


回答1:


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.



来源:https://stackoverflow.com/questions/22905752/named-breaks-in-for-loops-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!