Trait core::cmp::PartialOrd [] [src]

pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
    fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;

    fn lt(&self, other: &Rhs) -> bool { ... }
    fn le(&self, other: &Rhs) -> bool { ... }
    fn gt(&self, other: &Rhs) -> bool { ... }
    fn ge(&self, other: &Rhs) -> bool { ... }
}

Trait for values that can be compared for a sort-order.

The comparison must satisfy, for all a`a,`, b`band` and c`c`:

Note that these requirements mean that the trait itself must be implemented symmetrically and transitively: if T: PartialOrd<U>`T: PartialOrdand` and U: PartialOrd<V>`U: PartialOrdthen` then U: PartialOrd<T>`U: PartialOrdand` and T: PartialOrd<V>`T: PartialOrd`.

PartialOrd only requires implementation of the partial_cmp`partial_cmp` method, with the others generated from default implementations.

However it remains possible to implement the others separately for types which do not have a total order. For example, for floating point numbers, NaN < 0 == false`NaN < 0 == falseand` and NaN >= 0 == false`NaN >= 0 == false` (cf. IEEE 754-2008 section 5.11).

Required Methods

fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>

This method returns an ordering between self`selfand` and other`other` values if one exists.

Examples

fn main() { use std::cmp::Ordering; let result = 1.0.partial_cmp(&2.0); assert_eq!(result, Some(Ordering::Less)); let result = 1.0.partial_cmp(&1.0); assert_eq!(result, Some(Ordering::Equal)); let result = 2.0.partial_cmp(&1.0); assert_eq!(result, Some(Ordering::Greater)); }
use std::cmp::Ordering;

let result = 1.0.partial_cmp(&2.0);
assert_eq!(result, Some(Ordering::Less));

let result = 1.0.partial_cmp(&1.0);
assert_eq!(result, Some(Ordering::Equal));

let result = 2.0.partial_cmp(&1.0);
assert_eq!(result, Some(Ordering::Greater));

When comparison is impossible:

fn main() { let result = std::f64::NAN.partial_cmp(&1.0); assert_eq!(result, None); }
let result = std::f64::NAN.partial_cmp(&1.0);
assert_eq!(result, None);

Provided Methods

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self`selfand` and other`other) and is used by the`) and is used by the <`<` operator.

Examples

fn main() { use std::cmp::Ordering; let result = 1.0 < 2.0; assert_eq!(result, true); let result = 2.0 < 1.0; assert_eq!(result, false); }
use std::cmp::Ordering;

let result = 1.0 < 2.0;
assert_eq!(result, true);

let result = 2.0 < 1.0;
assert_eq!(result, false);

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self`selfand` and other`other) and is used by the`) and is used by the <=`<=` operator.

Examples

fn main() { let result = 1.0 <= 2.0; assert_eq!(result, true); let result = 2.0 <= 2.0; assert_eq!(result, true); }
let result = 1.0 <= 2.0;
assert_eq!(result, true);

let result = 2.0 <= 2.0;
assert_eq!(result, true);

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self`selfand` and other`other) and is used by the`) and is used by the >`>` operator.

Examples

fn main() { let result = 1.0 > 2.0; assert_eq!(result, false); let result = 2.0 > 2.0; assert_eq!(result, false); }
let result = 1.0 > 2.0;
assert_eq!(result, false);

let result = 2.0 > 2.0;
assert_eq!(result, false);

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self`selfand` and other`other) and is used by the`) and is used by the >=`>=` operator.

Examples

fn main() { let result = 2.0 >= 1.0; assert_eq!(result, true); let result = 2.0 >= 2.0; assert_eq!(result, true); }
let result = 2.0 >= 1.0;
assert_eq!(result, true);

let result = 2.0 >= 2.0;
assert_eq!(result, true);

Implementors