Protocol std::cmp::Comparable
Types that have a total order.
Types that implement this protocol can be compared using the <
, <=
, >
and >=
operators.
Method compare must be implemented, other methods can be mixed in. This includes the default implementations for equals and not_equals of the Equatable protocol.
Example
struct FullName { first: &[u8], last: &[u8] }
impl FullName {
use std::cmp::{Ordering, Comparable};
fn new(first: &[u8], last: &[u8]) -> FullName {
FullName { first: first, last: last }
}
fn compare(self: &FullName, other: &FullName) -> Ordering {
let first_cmp = self.first.compare(&other.first);
if first_cmp != Ordering::Equal {
return first_cmp;
}
self.last.compare(&other.last)
}
mixin Comparable<FullName>;
}
assert!(FullName::new("John", "Malkovich") < FullName::new("John", "McCain"));
assert!(FullName::new("John", "Doe") == FullName::new("John", "Doe"));
Run this example
See also lex_compare for a convenient way to implement the compare
method.
Required methods
Provided methods
-
Returns
true
iflhs
is equal torhs
,false
otherwise -
Returns
true
iflhs
not equal torhs
,false
otherwise -
Returns
true
iflhs
is strictly less thanrhs
,false
otherwise -
Returns
true
iflhs
is less or equal torhs
,false
otherwise -
Returns
true
iflhs
strictly greater thanrhs
,false
otherwise -
Returns
true
iflhs
greater than or equalrhs
,false
otherwise