Ord

Ord is like PartialOrd, except that it doesn't allow the option of returning a "no relation" value; for any pair of values, either they are equal, or one is less than the other.

Ord is used by the compiler to implement the < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal) comparison operators.

If a data type has the Ord trait, it has to also have the PartialOrd, Eq, and PartialEq traits. Like those traits, it can be manually implemented to enable comparisons between different data types, but we have to be very careful that the results returned by the various functions that are used to implement those traits agree with each other. When we derive the traits, we don't have to worry about that.

Here's an example of deriving Ord:

#[derive(Ord, Eq, PartialOrd, PartialEq)]
pub enum OrdExample {
Good,
Bad,
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.117.214