Trait std::cmp::PartialOrd
[−]
[src]
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 { ... }
}
Trait for values that can be compared for a sort-order.
The comparison must satisfy, for all a
, b
and c
:
- antisymmetry: if
a < b
then!(a > b)
, as well asa > b
implying!(a < b)
; and - transitivity:
a < b
andb < c
impliesa < c
. The same must hold for both==
and>
.
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>
.
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).
This trait can be used with #[derive]
. When derive
d, it will produce an ordering
based on the top-to-bottom declaration order of the struct's members.
Examples
fn main() { let x : u32 = 0; let y : u32 = 1; assert_eq!(x < y, true); assert_eq!(x.lt(&y), true); }let x : u32 = 0; let y : u32 = 1; assert_eq!(x < y, true); assert_eq!(x.lt(&y), true);
Required Methods
fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>
This method returns an ordering between self
and 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
and other
) and is used by the <
operator.
Examples
fn main() { let result = 1.0 < 2.0; assert_eq!(result, true); let result = 2.0 < 1.0; assert_eq!(result, false); }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
and other
) 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
and other
) 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
and other
) 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
impl<A, B, C, D, E, F, G, H, I, J, K, L> PartialOrd for (A, B, C, D, E, F, G, H, I, J, K, L) where F: PartialEq<F> + PartialOrd<F>, C: PartialEq<C> + PartialOrd<C>, L: PartialEq<L> + PartialOrd<L>, H: PartialEq<H> + PartialOrd<H>, J: PartialEq<J> + PartialOrd<J>, A: PartialEq<A> + PartialOrd<A>, I: PartialEq<I> + PartialOrd<I>, B: PartialEq<B> + PartialOrd<B>, E: PartialEq<E> + PartialOrd<E>, D: PartialEq<D> + PartialOrd<D>, G: PartialEq<G> + PartialOrd<G>, K: PartialEq<K> + PartialOrd<K>
impl<A, B, C, D, E, F, G, H, I, J, K> PartialOrd for (A, B, C, D, E, F, G, H, I, J, K) where B: PartialEq<B> + PartialOrd<B>, A: PartialEq<A> + PartialOrd<A>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, J: PartialEq<J> + PartialOrd<J>, H: PartialEq<H> + PartialOrd<H>, C: PartialEq<C> + PartialOrd<C>, I: PartialEq<I> + PartialOrd<I>, G: PartialEq<G> + PartialOrd<G>, K: PartialEq<K> + PartialOrd<K>, D: PartialEq<D> + PartialOrd<D>
impl<A, B, C, D, E, F, G, H, I, J> PartialOrd for (A, B, C, D, E, F, G, H, I, J) where F: PartialEq<F> + PartialOrd<F>, A: PartialEq<A> + PartialOrd<A>, C: PartialEq<C> + PartialOrd<C>, H: PartialEq<H> + PartialOrd<H>, I: PartialEq<I> + PartialOrd<I>, J: PartialEq<J> + PartialOrd<J>, B: PartialEq<B> + PartialOrd<B>, E: PartialEq<E> + PartialOrd<E>, D: PartialEq<D> + PartialOrd<D>, G: PartialEq<G> + PartialOrd<G>
impl<A, B, C, D, E, F, G, H, I> PartialOrd for (A, B, C, D, E, F, G, H, I) where B: PartialEq<B> + PartialOrd<B>, G: PartialEq<G> + PartialOrd<G>, D: PartialEq<D> + PartialOrd<D>, F: PartialEq<F> + PartialOrd<F>, C: PartialEq<C> + PartialOrd<C>, H: PartialEq<H> + PartialOrd<H>, A: PartialEq<A> + PartialOrd<A>, E: PartialEq<E> + PartialOrd<E>, I: PartialEq<I> + PartialOrd<I>
impl<A, B, C, D, E, F, G, H> PartialOrd for (A, B, C, D, E, F, G, H) where F: PartialEq<F> + PartialOrd<F>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, A: PartialEq<A> + PartialOrd<A>, E: PartialEq<E> + PartialOrd<E>, H: PartialEq<H> + PartialOrd<H>, G: PartialEq<G> + PartialOrd<G>, D: PartialEq<D> + PartialOrd<D>
impl<A, B, C, D, E, F, G> PartialOrd for (A, B, C, D, E, F, G) where G: PartialEq<G> + PartialOrd<G>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, C: PartialEq<C> + PartialOrd<C>, B: PartialEq<B> + PartialOrd<B>, A: PartialEq<A> + PartialOrd<A>, D: PartialEq<D> + PartialOrd<D>
impl<A, B, C, D, E, F> PartialOrd for (A, B, C, D, E, F) where A: PartialEq<A> + PartialOrd<A>, F: PartialEq<F> + PartialOrd<F>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, E: PartialEq<E> + PartialOrd<E>, D: PartialEq<D> + PartialOrd<D>
impl<A, B, C, D, E> PartialOrd for (A, B, C, D, E) where A: PartialEq<A> + PartialOrd<A>, C: PartialEq<C> + PartialOrd<C>, B: PartialEq<B> + PartialOrd<B>, E: PartialEq<E> + PartialOrd<E>, D: PartialEq<D> + PartialOrd<D>
impl<A, B, C, D> PartialOrd for (A, B, C, D) where B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, A: PartialEq<A> + PartialOrd<A>, D: PartialEq<D> + PartialOrd<D>
impl<A, B, C> PartialOrd for (A, B, C) where A: PartialEq<A> + PartialOrd<A>, C: PartialEq<C> + PartialOrd<C>, B: PartialEq<B> + PartialOrd<B>
impl<A, B> PartialOrd for (A, B) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>
impl<A> PartialOrd for (A,) where A: PartialEq<A> + PartialOrd<A>
impl PartialOrd for str
impl<T> PartialOrd for [T] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 32] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 31] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 30] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 29] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 28] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 27] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 26] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 25] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 24] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 23] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 22] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 21] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 20] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 19] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 18] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 17] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 16] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 15] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 14] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 13] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 12] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 11] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 10] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 9] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 8] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 7] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 6] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 5] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 4] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 3] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 2] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 1] where T: PartialOrd<T>
impl<T> PartialOrd for [T; 0] where T: PartialOrd<T>
impl PartialOrd for i64
impl PartialOrd for i32
impl PartialOrd for i16
impl PartialOrd for i8
impl PartialOrd for isize
impl PartialOrd for u64
impl PartialOrd for u32
impl PartialOrd for u16
impl PartialOrd for u8
impl PartialOrd for usize
impl PartialOrd for char
impl PartialOrd for f64
impl PartialOrd for f32
impl PartialOrd for bool
impl PartialOrd for ()
impl<T> PartialOrd for *mut T where T: ?Sized
impl<T> PartialOrd for *const T where T: ?Sized
impl PartialOrd for Ordering
impl<T> PartialOrd for PhantomData<T> where T: ?Sized
impl<T, E> PartialOrd for Result<T, E> where T: PartialOrd<T>, E: PartialOrd<E>
impl<T> PartialOrd for Option<T> where T: PartialOrd<T>
impl<T> PartialOrd for Box<T> where T: PartialOrd<T> + ?Sized
impl<T> PartialOrd for Rc<T> where T: PartialOrd<T> + ?Sized
impl<'a, B> PartialOrd for Cow<'a, B> where B: PartialOrd<B> + ToOwned + ?Sized
impl PartialOrd for Error
impl PartialOrd for String
impl<T> PartialOrd for Vec<T> where T: PartialOrd<T>
impl<T> PartialOrd for Wrapping<T> where T: PartialOrd<T>
impl<K, V> PartialOrd for BTreeMap<K, V> where V: PartialOrd<V>, K: PartialOrd<K>
impl<T> PartialOrd for BTreeSet<T> where T: PartialOrd<T>
impl<A> PartialOrd for LinkedList<A> where A: PartialOrd<A>
impl<A> PartialOrd for VecDeque<A> where A: PartialOrd<A>
impl PartialOrd for CString
impl PartialOrd for CStr
impl PartialOrd for OsString
impl PartialOrd for OsString
impl PartialOrd for OsStr
impl PartialOrd for OsStr
impl<'a, 'b> PartialOrd for OsString
impl<'a, 'b> PartialOrd for OsStr
impl<'a, 'b> PartialOrd for OsString
impl<'a, 'b> PartialOrd for &'a OsStr
impl<'a, 'b> PartialOrd for Cow<'a, OsStr>
impl<'a, 'b> PartialOrd for OsStr
impl<'a, 'b> PartialOrd for Cow<'a, OsStr>
impl<'a, 'b> PartialOrd for &'b OsStr
impl<'a, 'b> PartialOrd for Cow<'a, OsStr>
impl<'a, 'b> PartialOrd for OsString
impl PartialOrd for IpAddr
impl PartialOrd for Ipv4Addr
impl PartialOrd for Ipv6Addr
impl<'a> PartialOrd for Prefix<'a>
impl<'a> PartialOrd for PrefixComponent<'a>
impl<'a> PartialOrd for Component<'a>
impl<'a> PartialOrd for Components<'a>
impl PartialOrd for PathBuf
impl PartialOrd for Path
impl<'a, 'b> PartialOrd for PathBuf
impl<'a, 'b> PartialOrd for Path
impl<'a, 'b> PartialOrd for PathBuf
impl<'a, 'b> PartialOrd for &'a Path
impl<'a, 'b> PartialOrd for Cow<'a, Path>
impl<'a, 'b> PartialOrd for Path
impl<'a, 'b> PartialOrd for Cow<'a, Path>
impl<'a, 'b> PartialOrd for &'b Path
impl<'a, 'b> PartialOrd for Cow<'a, Path>
impl<'a, 'b> PartialOrd for PathBuf
impl<'a, 'b> PartialOrd for PathBuf
impl<'a, 'b> PartialOrd for OsStr
impl<'a, 'b> PartialOrd for PathBuf
impl<'a, 'b> PartialOrd for &'a OsStr
impl<'a, 'b> PartialOrd for PathBuf
impl<'a, 'b> PartialOrd for Cow<'a, OsStr>
impl<'a, 'b> PartialOrd for PathBuf
impl<'a, 'b> PartialOrd for OsString
impl<'a, 'b> PartialOrd for Path
impl<'a, 'b> PartialOrd for OsStr
impl<'a, 'b> PartialOrd for Path
impl<'a, 'b> PartialOrd for &'a OsStr
impl<'a, 'b> PartialOrd for Path
impl<'a, 'b> PartialOrd for Cow<'a, OsStr>
impl<'a, 'b> PartialOrd for Path
impl<'a, 'b> PartialOrd for OsString
impl<'a, 'b> PartialOrd for &'a Path
impl<'a, 'b> PartialOrd for OsStr
impl<'a, 'b> PartialOrd for &'a Path
impl<'a, 'b> PartialOrd for Cow<'b, OsStr>
impl<'a, 'b> PartialOrd for &'a Path
impl<'a, 'b> PartialOrd for OsString
impl<'a, 'b> PartialOrd for Cow<'a, Path>
impl<'a, 'b> PartialOrd for OsStr
impl<'a, 'b> PartialOrd for Cow<'a, Path>
impl<'a, 'b> PartialOrd for &'b OsStr
impl<'a, 'b> PartialOrd for Cow<'a, Path>
impl<'a, 'b> PartialOrd for OsString
impl<T> PartialOrd for Arc<T> where T: PartialOrd<T> + ?Sized
impl PartialOrd for Duration
impl PartialOrd for Instant
impl PartialOrd for SystemTime