从闭包内部使用 continue 的 Rust 方式是什么? [英] What is the Rust way of using continue from inside a closure?

查看:57
本文介绍了从闭包内部使用 continue 的 Rust 方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不可能的,但非常需要:

This isn't possible, but very much desired:

loop {
    something().unwrap_or_else(|err| {
        warn!("Something bad happened: {}", err);
        continue;
    });

    // other stuff
}

Rust 的解决方法是什么?

What is the Rust way of solving it?

推荐答案

unwrap_or_else 只是一个围绕 match 的便捷方法,通常用于方法调用链.由于这里不是这种情况,您可以简单地使用 match 代替,并且由于您似乎只对 Err 案例感兴趣,因此您也可以使用 如果让:

unwrap_or_else is just a convenience method around a match usually used in method call chains. As this is not the case here, you can simply use a match instead, and since you only seem to be interested by the Err case, you can also use if let:

loop {
    if let Err(err) = something() {
        warn!("Something bad happened: {}", err);
        continue;
    }

    // other stuff
}

这篇关于从闭包内部使用 continue 的 Rust 方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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