Rust 中的逻辑运算符会短路吗? [英] Do logical operators short-circuit in Rust?

查看:53
本文介绍了Rust 中的逻辑运算符会短路吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

&&|| 是否会在知道结果后立即停止计算?换句话说将 (true == true) ||(true == false) 不对右侧求值,因为在仅对左侧求值后,整个表达式已知为 true.

Do && and || stop evaluating as soon as a result is known? In other words will (true == true) || (true == false) not evaluate the right side because the whole expression is known to be true after only evaluating the left side.

推荐答案

是.

来自 Rust 参考:

fn main() {
    let x = false || true; // true
    let y = false && panic!(); // false, doesn't evaluate `panic!()`
}

这篇关于Rust 中的逻辑运算符会短路吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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