在Rust中命名为循环 [英] Named breaks in for loops in Rust

查看:134
本文介绍了在Rust中命名为循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Rust中为循环嵌套,然后从内部的那个循环中打破外部循环。在Java中?我知道Rust在 loop 中支持命名中断,但似乎无法找到关于对于的相同信息。

解决方案

是的。它使用与生存时间相同的语法。


$ b

  fn main(){
'outer :范围内的x(0u,5){
'inner:范围内的y(0u,5){
println!({},{},x,y);
如果y == 3 {
break'outer;



$ b code


请参阅循环标签文档


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.

解决方案

Yes. It uses the same syntax as lifetimes.

fn main() {
    'outer: for x in range(0u, 5) {
        'inner: for y in range(0u, 5) {
            println!("{},{}", x, y);
            if y == 3 {
                break 'outer;
            }
        }
    }
}

See loop labels documentation.

这篇关于在Rust中命名为循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆