pub trait PartialOrd<Rhs = Self>: PartialEq<Rhs> where
    Rhs: ?Sized
{ 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 { ... } }
Expand description

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

The lt, le, gt, and ge methods of this trait can be called using the <, <=, >, and >= operators, respectively.

The methods of this trait must be consistent with each other and with those of PartialEq in the following sense:

  • a == b if and only if partial_cmp(a, b) == Some(Equal).
  • a < b if and only if partial_cmp(a, b) == Some(Less) (ensured by the default implementation).
  • a > b if and only if partial_cmp(a, b) == Some(Greater) (ensured by the default implementation).
  • a <= b if and only if a < b || a == b (ensured by the default implementation).
  • a >= b if and only if a > b || a == b (ensured by the default implementation).
  • a != b if and only if !(a == b) (already part of PartialEq).

If Ord is also implemented for Self and Rhs, it must also be consistent with partial_cmp (see the documentation of that trait for the exact requirements). It’s easy to accidentally make them disagree by deriving some of the traits and manually implementing others.

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

  • transitivity: a < b and b < c implies a < c. The same must hold for both == and >.
  • duality: a < b if and only if b > a.

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

Corollaries

The following corollaries follow from the above requirements:

  • irreflexivity of < and >: !(a < a), !(a > a)
  • transitivity of >: if a > b and b > c then a > c
  • duality of partial_cmp: partial_cmp(a, b) == partial_cmp(b, a).map(Ordering::reverse)

Derivable

This trait can be used with #[derive].

When derived on structs, it will produce a lexicographic ordering based on the top-to-bottom declaration order of the struct’s members.

When derived on enums, variants are ordered by their discriminants. By default, the discriminant is smallest for variants at the top, and largest for variants at the bottom. Here’s an example:

#[derive(PartialEq, PartialOrd)]
enum E {
    Top,
    Bottom,
}

assert!(E::Top < E::Bottom);

However, manually setting the discriminants can override this default behavior:

#[derive(PartialEq, PartialOrd)]
enum E {
    Top = 2,
    Bottom = 1,
}

assert!(E::Bottom < E::Top);

How can I implement PartialOrd?

PartialOrd only requires implementation of the 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 and NaN >= 0 == false (cf. IEEE 754-2008 section 5.11).

PartialOrd requires your type to be PartialEq.

If your type is Ord, you can implement partial_cmp by using cmp:

use std::cmp::Ordering;

#[derive(Eq)]
struct Person {
    id: u32,
    name: String,
    height: u32,
}

impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Person {
    fn cmp(&self, other: &Self) -> Ordering {
        self.height.cmp(&other.height)
    }
}

impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.height == other.height
    }
}

You may also find it useful to use partial_cmp on your type’s fields. Here is an example of Person types who have a floating-point height field that is the only field to be used for sorting:

use std::cmp::Ordering;

struct Person {
    id: u32,
    name: String,
    height: f64,
}

impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.height.partial_cmp(&other.height)
    }
}

impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.height == other.height
    }
}

Examples

let x: u32 = 0;
let y: u32 = 1;

assert_eq!(x < y, true);
assert_eq!(x.lt(&y), true);

Required methods

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

Examples
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:

let result = f64::NAN.partial_cmp(&1.0);
assert_eq!(result, None);

Provided methods

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

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

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

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

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

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

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

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

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

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

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

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

Implementations on Foreign Types

Implements comparison of vectors lexicographically.

Implements comparison operations on strings.

Strings are compared lexicographically by their byte values. This compares Unicode code points based on their positions in the code charts. This is not necessarily the same as “alphabetical” order, which varies by language and locale. Comparing strings according to culturally-accepted standards requires locale-specific data that is outside the scope of the str type.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Panics

Panics if the value in either RefCell is currently borrowed.

Partial comparison for two Arcs.

The two are compared by calling partial_cmp() on their inner values.

Examples
use std::sync::Arc;
use std::cmp::Ordering;

let five = Arc::new(5);

assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6)));

Less-than comparison for two Arcs.

The two are compared by calling < on their inner values.

Examples
use std::sync::Arc;

let five = Arc::new(5);

assert!(five < Arc::new(6));

‘Less than or equal to’ comparison for two Arcs.

The two are compared by calling <= on their inner values.

Examples
use std::sync::Arc;

let five = Arc::new(5);

assert!(five <= Arc::new(5));

Greater-than comparison for two Arcs.

The two are compared by calling > on their inner values.

Examples
use std::sync::Arc;

let five = Arc::new(5);

assert!(five > Arc::new(4));

‘Greater than or equal to’ comparison for two Arcs.

The two are compared by calling >= on their inner values.

Examples
use std::sync::Arc;

let five = Arc::new(5);

assert!(five >= Arc::new(5));

Partial comparison for two Rcs.

The two are compared by calling partial_cmp() on their inner values.

Examples
use std::rc::Rc;
use std::cmp::Ordering;

let five = Rc::new(5);

assert_eq!(Some(Ordering::Less), five.partial_cmp(&Rc::new(6)));

Less-than comparison for two Rcs.

The two are compared by calling < on their inner values.

Examples
use std::rc::Rc;

let five = Rc::new(5);

assert!(five < Rc::new(6));

‘Less than or equal to’ comparison for two Rcs.

The two are compared by calling <= on their inner values.

Examples
use std::rc::Rc;

let five = Rc::new(5);

assert!(five <= Rc::new(5));

Greater-than comparison for two Rcs.

The two are compared by calling > on their inner values.

Examples
use std::rc::Rc;

let five = Rc::new(5);

assert!(five > Rc::new(4));

‘Greater than or equal to’ comparison for two Rcs.

The two are compared by calling >= on their inner values.

Examples
use std::rc::Rc;

let five = Rc::new(5);

assert!(five >= Rc::new(5));

Order proxy references by the value of their proxied bit.

To order by address, decay to a BitPtr with into_bitptr.

Compares two BitSlices by semantic — not bitwise — ordering.

The comparison sorts by testing at each index if one slice has a high bit where the other has a low. At the first index where the slices differ, the slice with the high bit is greater. If the slices are equal until at least one terminates, then they are compared by length.

Implementors

Implements comparison of vectors, lexicographically.

impl<T, const CAP: usize> PartialOrd<ArrayVec<T, CAP>> for ArrayVec<T, CAP> where
    T: PartialOrd

impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for ArrayString<CAP>

impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>

impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str

impl<O, V, T> PartialOrd<BitArray<O, V>> for BitSlice<O, T> where
    O: BitOrder,
    V: BitView,
    T: BitStore

impl<O, V, Rhs> PartialOrd<Rhs> for BitArray<O, V> where
    O: BitOrder,
    V: BitView,
    Rhs: ?Sized,
    BitSlice<O, V::Store>: PartialOrd<Rhs>, 

impl<R: PartialOrd> PartialOrd<BitIdx<R>> for BitIdx<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitIdxError<R>> for BitIdxError<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitTail<R>> for BitTail<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitPos<R>> for BitPos<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitSel<R>> for BitSel<R> where
    R: BitRegister

impl<R: PartialOrd> PartialOrd<BitMask<R>> for BitMask<R> where
    R: BitRegister

impl PartialOrd<Mut> for Mut

impl PartialOrd<Lsb0> for Lsb0

impl PartialOrd<Msb0> for Msb0

impl<M1, M2, T1, T2> PartialOrd<Address<M2, T2>> for Address<M1, T1> where
    M1: Mutability,
    M2: Mutability,
    T1: BitStore,
    T2: BitStore

impl<T: PartialOrd> PartialOrd<AddressError<T>> for AddressError<T> where
    T: BitStore

impl<M1, M2, O1, O2, T1, T2> PartialOrd<BitRef<'_, M2, O2, T2>> for BitRef<'_, M1, O1, T1> where
    M1: Mutability,
    M2: Mutability,
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<M, O, T> PartialOrd<bool> for BitRef<'_, M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<M, O, T> PartialOrd<&'_ bool> for BitRef<'_, M, O, T> where
    M: Mutability,
    O: BitOrder,
    T: BitStore

impl<M1, M2, O, T1, T2> PartialOrd<BitPtr<M2, O, T2>> for BitPtr<M1, O, T1> where
    M1: Mutability,
    M2: Mutability,
    O: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<T: PartialOrd> PartialOrd<BitPtrError<T>> for BitPtrError<T> where
    T: BitStore,
    T::Mem: PartialOrd

impl<T: PartialOrd> PartialOrd<BitSpanError<T>> for BitSpanError<T> where
    T: BitStore

impl<'a, O: PartialOrd, T: PartialOrd> PartialOrd<IterOnes<'a, O, T>> for IterOnes<'a, O, T> where
    O: BitOrder,
    T: BitStore

impl<'a, O: PartialOrd, T: PartialOrd> PartialOrd<IterZeros<'a, O, T>> for IterZeros<'a, O, T> where
    O: BitOrder,
    T: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitSlice<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitSlice<O2, T2>> for &BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitSlice<O2, T2>> for &mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ BitSlice<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ mut BitSlice<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ mut BitSlice<O2, T2>> for &BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<&'_ BitSlice<O2, T2>> for &mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O, T, Rhs> PartialOrd<Rhs> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore,
    Rhs: ?Sized + PartialOrd<BitSlice<O, T>>, 

impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<'a, O1, O2, T1, T2> PartialOrd<BitBox<O2, T2>> for &'a mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O1, O2, T1, T2> PartialOrd<BitVec<O2, T2>> for BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<'a, O1, O2, T1, T2> PartialOrd<BitVec<O2, T2>> for &'a BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<'a, O1, O2, T1, T2> PartialOrd<BitVec<O2, T2>> for &'a mut BitSlice<O1, T1> where
    O1: BitOrder,
    O2: BitOrder,
    T1: BitStore,
    T2: BitStore

impl<O, T, Rhs> PartialOrd<Rhs> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore,
    Rhs: ?Sized + PartialOrd<BitSlice<O, T>>, 

impl<Tz: TimeZone> PartialOrd<Date<Tz>> for Date<Tz>

impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd<DateTime<Tz2>> for DateTime<Tz>

impl<L: PartialOrd, R: PartialOrd> PartialOrd<Either<L, R>> for Either<L, R>

impl<K, V, S> PartialOrd<BoundedBTreeMap<K, V, S>> for BoundedBTreeMap<K, V, S> where
    BTreeMap<K, V>: PartialOrd

impl<T, S> PartialOrd<BoundedBTreeSet<T, S>> for BoundedBTreeSet<T, S> where
    BTreeSet<T>: PartialOrd

impl<T: PartialOrd, N> PartialOrd<GenericArray<T, N>> for GenericArray<T, N> where
    N: ArrayLength<T>, 

impl PartialOrd<DwUt> for DwUt

impl PartialOrd<DwAt> for DwAt

impl PartialOrd<DwDs> for DwDs

impl PartialOrd<DwId> for DwId

impl PartialOrd<DwCc> for DwCc

impl PartialOrd<DwOp> for DwOp

impl<'a> PartialOrd<Metadata<'a>> for Metadata<'a>

impl<T, R: Dim, C: Dim, S> PartialOrd<Matrix<T, R, C, S>> for Matrix<T, R, C, S> where
    T: Scalar + PartialOrd,
    S: Storage<T, R, C>, 

impl<T: Scalar + PartialOrd, const D: usize> PartialOrd<Point<T, D>> for Point<T, D>

impl<T: Clone + Integer> PartialOrd<Ratio<T>> for Ratio<T>

impl<T: PartialOrd> PartialOrd<Compact<T>> for Compact<T>

impl PartialOrd<U128> for U128

impl PartialOrd<U256> for U256

impl PartialOrd<U512> for U512

impl PartialOrd<H128> for H128

impl PartialOrd<H160> for H160

impl PartialOrd<H256> for H256

impl PartialOrd<H512> for H512

impl PartialOrd<Span> for Span

impl<A: Array> PartialOrd<SmallVec<A>> for SmallVec<A> where
    A::Item: PartialOrd

impl<T: Into<u64> + Copy> PartialOrd<T> for Timestamp

impl<A: Array> PartialOrd<ArrayVec<A>> for ArrayVec<A> where
    A::Item: PartialOrd

impl<'s, T> PartialOrd<SliceVec<'s, T>> for SliceVec<'s, T> where
    T: PartialOrd

impl<A: Array> PartialOrd<TinyVec<A>> for TinyVec<A> where
    A::Item: PartialOrd

impl<T: PartialOrd> PartialOrd<Spanned<T>> for Spanned<T>

impl<'a> PartialOrd<NibbleSlice<'a>> for NibbleSlice<'a>

impl PartialOrd<B0> for B0

impl PartialOrd<B1> for B1

impl<U: PartialOrd + Unsigned + NonZero> PartialOrd<PInt<U>> for PInt<U>

impl<U: PartialOrd + Unsigned + NonZero> PartialOrd<NInt<U>> for NInt<U>

impl PartialOrd<Z0> for Z0

impl<U: PartialOrd, B: PartialOrd> PartialOrd<UInt<U, B>> for UInt<U, B>

impl<V: PartialOrd, A: PartialOrd> PartialOrd<TArr<V, A>> for TArr<V, A>

impl PartialOrd<Less> for Less

impl<T: Into<F32> + Copy> PartialOrd<T> for F32

impl<T: Into<F64> + Copy> PartialOrd<T> for F64