12

I want to define Ord for a custom type Point so it is sorted by the distance to the origin for Advent of Code 2019 day 10:

impl std::cmp::Ord for Point {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        let this = self.x * self.x + self.y + self.y;
        let that = other.x * other.x + other.y * other.y;
        return this.cmp(&that);
    }
}

However, I ran into this compile error:

= help: the trait std::cmp::PartialOrd is not implemented for helpers::models::Point

The documentation for PartialOrd only explains how to derive it or implement it. Those are pretty clear.

Why does Ord depend on this? What does the name Partial imply in the trait name? When will it get used?

Endzeit
  • 4,810
  • 5
  • 29
  • 52
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 1
    The difference is described in the first sentence of both pages of documentation: *[PartialOrd is] for values that can be compared for a sort-order.*; *[Ord is] for types that form a total order.* – Shepmaster Dec 13 '19 at 02:33
  • 1
    Ok @Shepmaster your answer on the other question explains it well. I didn't know the concept of total order and partial order before. TIL. Thanks! – Yuchen Dec 13 '19 at 02:37

0 Answers0