How does this expression evaluate?

Hi, today for the first time I ran into a problem where rust did not behave as expected. However I don't understand how these two expressions are evaluated to come to those results.

fn main() {
    dbg!(! 0 < 1);  // [test.rs:2] !0 < 1 = true
    dbg!(!(0 < 1)); // [test.rs:3] !(0 < 1) = false
}

I am probably just missing a feature that makes it behave that way, can someone help me out?

Due to expression precedence ! 0 < 1 is evaluated as (!0) < 1. By default integers are i32 and the ! operator inverts all bits of the operand which makes this -1 < 1.

6 Likes

Thanks, that was exactly the response I was looking for! I did not know that this worked.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.