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
trueiflhsis equal torhs,falseotherwise -
Returns
trueiflhsnot equal torhs,falseotherwise -
Returns
trueiflhsis strictly less thanrhs,falseotherwise -
Returns
trueiflhsis less or equal torhs,falseotherwise -
Returns
trueiflhsstrictly greater thanrhs,falseotherwise -
Returns
trueiflhsgreater than or equalrhs,falseotherwise