Module std::cmpStable
[-]
[+]
[src]
Defines the PartialOrd
and PartialEq
comparison traits.
This module defines both PartialOrd
and PartialEq
traits which are used by the
compiler to implement comparison operators. Rust programs may implement
PartialOrd
to overload the <
, <=
, >
, and >=
operators, and may implement
PartialEq
to overload the ==
and !=
operators.
For example, to define a type with a customized definition for the PartialEq operators, you could do the following:
fn main() { use core::num::SignedInt; // Our type. struct SketchyNum { num : int } // Our implementation of `PartialEq` to support `==` and `!=`. impl PartialEq for SketchyNum { // Our custom eq allows numbers which are near each other to be equal! :D fn eq(&self, other: &SketchyNum) -> bool { (self.num - other.num).abs() < 5 } } // Now these binary operators will work when applied! assert!(SketchyNum {num: 37} == SketchyNum {num: 34}); assert!(SketchyNum {num: 25} != SketchyNum {num: 57}); }use core::num::SignedInt; // Our type. struct SketchyNum { num : int } // Our implementation of `PartialEq` to support `==` and `!=`. impl PartialEq for SketchyNum { // Our custom eq allows numbers which are near each other to be equal! :D fn eq(&self, other: &SketchyNum) -> bool { (self.num - other.num).abs() < 5 } } // Now these binary operators will work when applied! assert!(SketchyNum {num: 37} == SketchyNum {num: 34}); assert!(SketchyNum {num: 25} != SketchyNum {num: 57});
Enums
Ordering | An ordering is, e.g, a result of a comparison between two values. |
Traits
Eq | Trait for equality comparisons which are equivalence relations. |
Equiv | The equivalence relation. Two values may be equivalent even if they are
of different types. The most common use case for this relation is
container types; e.g. it is often desirable to be able to use |
Ord | Trait for types that form a total order. |
PartialEq | Trait for equality comparisons which are partial equivalence relations. |
PartialOrd | Trait for values that can be compared for a sort-order. |
Functions
max | Compare and return the maximum of two values. |
min | Compare and return the minimum of two values. |
partial_max | Compare and return the maximum of two values if there is one. |
partial_min | Compare and return the minimum of two values if there is one. |