Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do logical operators short-circuit in Rust?

Tags:

rust

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.

like image 545
user Avatar asked Dec 06 '18 04:12

user


People also ask

Does rust have short-circuiting?

Rust has a great story around short circuiting. And it's not just with return , break , and continue .

Are logical operators evaluated with short-circuit?

Do logical operators in the C language are evaluated with the short circuit? Explanation: None.

Why logical operators are called short-circuit?

In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned.

Which operator is a non short-circuiting logical?

The | and & logical operators, known as non-short circuit operators, should not be used. Using a non-short circuit operator reduces the efficiency of the program, is potentially confusing and can even lead to the program crashing if the first operand acts as a safety check for the second.


1 Answers

Yes.

From the Rust reference:

fn main() {
    let x = false || true; // true
    let y = false && panic!(); // false, doesn't evaluate `panic!()`
}
like image 158
user Avatar answered Oct 07 '22 11:10

user