Partial Equal, Partial Order

The next four traits we’re going to deal with are about equality and ordering. Rust breaks each of these down into partial and total traits. The reason for this has to do with mathematical theory, reflexivity and equality, and the way that computers represent non-integer values (like f32 and f64). The details aren’t vital to our understanding, so we’re going to skip over them. If you’re curious, Wikipedia has some articles on the top:

For us, the important bit is:

  • If you want to use == and !=, you need to derive the PartialEq trait

  • If you derive the PartialEq trait, and are allowed to derive the Eq trait, you should derive that one too

  • If you want to use the comparison operators, you need to derive both the PartialEq and PartialOrd traits

  • If you derive those two traits and are allowed to derive the Eq and Ord traits, you should derive those two as well

Enough theory! Let’s see some of this in practice:

Get hands-on with 1200+ tech skills courses.