user
user

Reputation: 711

Do logical operators short-circuit in 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.

Upvotes: 25

Views: 8291

Answers (1)

user
user

Reputation: 711

Yes.

From the Rust reference:

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

Upvotes: 36

Related Questions