Enum std::cmp::Ordering1.0.0 [] [src]

pub enum Ordering {
    Less,
    Equal,
    Greater,
}
1.0.0

An Ordering is the result of a comparison between two values.

Examples

fn main() { use std::cmp::Ordering; let result = 1.cmp(&2); assert_eq!(Ordering::Less, result); let result = 1.cmp(&1); assert_eq!(Ordering::Equal, result); let result = 2.cmp(&1); assert_eq!(Ordering::Greater, result); }
use std::cmp::Ordering;

let result = 1.cmp(&2);
assert_eq!(Ordering::Less, result);

let result = 1.cmp(&1);
assert_eq!(Ordering::Equal, result);

let result = 2.cmp(&1);
assert_eq!(Ordering::Greater, result);Run

Variants

An ordering where a compared value is less [than another].

An ordering where a compared value is equal [to another].

An ordering where a compared value is greater [than another].

Methods

impl Ordering

Reverse the Ordering.

  • Less becomes Greater.
  • Greater becomes Less.
  • Equal becomes Equal.

Examples

Basic behavior:

fn main() { use std::cmp::Ordering; assert_eq!(Ordering::Less.reverse(), Ordering::Greater); assert_eq!(Ordering::Equal.reverse(), Ordering::Equal); assert_eq!(Ordering::Greater.reverse(), Ordering::Less); }
use std::cmp::Ordering;

assert_eq!(Ordering::Less.reverse(), Ordering::Greater);
assert_eq!(Ordering::Equal.reverse(), Ordering::Equal);
assert_eq!(Ordering::Greater.reverse(), Ordering::Less);Run

This method can be used to reverse a comparison:

fn main() { let mut data: &mut [_] = &mut [2, 10, 5, 8]; // sort the array from largest to smallest. data.sort_by(|a, b| a.cmp(b).reverse()); let b: &mut [_] = &mut [10, 8, 5, 2]; assert!(data == b); }
let mut data: &mut [_] = &mut [2, 10, 5, 8];

// sort the array from largest to smallest.
data.sort_by(|a, b| a.cmp(b).reverse());

let b: &mut [_] = &mut [10, 8, 5, 2];
assert!(data == b);Run

Trait Implementations

impl PartialEq<Ordering> for Ordering

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Clone for Ordering

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Ord for Ordering

This method returns an Ordering between self and other. Read more

impl Debug for Ordering

Formats the value using the given formatter.

impl Eq for Ordering

impl Copy for Ordering

impl Hash for Ordering

Feeds this value into the state given, updating the hasher as necessary.

Feeds a slice of this type into the state provided.

impl PartialOrd<Ordering> for Ordering

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

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

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

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

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