Comparison Operators

We’ve seen == and < so far. Let’s document all six of the comparison operators we have at our disposal.

  • ==
    equals

  • !=
    not equals

  • >
    greater than

  • <
    less than

  • >=
    greater than or equals

  • <=
    less than or equals

Comparison operators can work on many different types. The exact rules are a bit beyond us right now, as they tie into traits, which we haven’t learned yet. For now, it’s enough to know that:

  • Strings, numbers, Booleans, and units can all be compared with equals and not equals.

  • Numbers can all be compared with the other four operators as well.

And we can see these in action using assert! together with assert_eq! and its not-equal counterpart assert_ne!: