cmp
import std::core::cmp; — in the prelude · source
Eq
type trait Eq {
equals(&this, other: &This) -> boolean;
}
Equality. Implement equals and == / != on the type route through it. Every primitive has an impl; the containers implement it parametrically — an Array<T> is Eq when T is.
Floats compare bitwise
This is the one place where core deliberately departs from IEEE-754. Eq for f32 and f64 compares the raw bits, and Ord orders them as sign-magnitude integers:
-NaN < -inf < ... < -0.0 < +0.0 < ... < +inf < +NaN
That keeps Eq a genuine equivalence relation — reflexive even for NaN — and keeps it consistent with Ord, which IEEE comparison cannot be (NaN is unordered and -0.0 == 0.0). Two consequences worth knowing: (0.0).equals(&-0.0) is false, and two NaNs with different payloads are unequal. When you want IEEE semantics, use the == operator directly.
Implementors
implement trait Eq for i8
implement trait Eq for i16
implement trait Eq for i32
implement trait Eq for i64
implement trait Eq for u8
implement trait Eq for u16
implement trait Eq for u32
implement trait Eq for u64
implement trait Eq for i128
implement trait Eq for u128
implement trait Eq for usize
implement trait Eq for isize
implement trait Eq for boolean
implement trait Eq for char
implement trait Eq for string
implement trait Eq for f32
implement trait Eq for f64
implement<T, A> trait Eq for struct Array<T, A>
where T: Eq, A: Allocator // std::collections::array
implement<A, B> trait Eq for struct Pair<A, B>
where A: Eq, B: Eq // std::collections::pair
implement trait Eq for struct Str // std::collections::str
implement<A> trait Eq for struct String<A>
where A: Allocator // std::collections::string
implement<T> trait Eq for enum Option<T>
where T: Eq // std::core::option
implement<T, E> trait Eq for enum Result<T, E>
where T: Eq, E: Eq // std::core::result
implement trait Eq for enum RandomError // std::random::error
implement trait Eq for struct Instant // std::time::clock
implement trait Eq for struct Duration // std::time::duration
Ord
type trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}
A total order, extending Eq. The relational operators are sugar over it: on an Ord type, a < b is rewritten to a.compare(&b).is_lt(). Implement compare and a type gets <, >, <=, and >= for free. boolean has Eq but not Ord; string compares as a C string, by bytes up to the first NUL.
Implementors
implement trait Ord for i8
implement trait Ord for i16
implement trait Ord for i32
implement trait Ord for i64
implement trait Ord for u8
implement trait Ord for u16
implement trait Ord for u32
implement trait Ord for u64
implement trait Ord for i128
implement trait Ord for u128
implement trait Ord for usize
implement trait Ord for isize
implement trait Ord for char
implement trait Ord for string
implement trait Ord for f32
implement trait Ord for f64
implement trait Ord for struct Str // std::collections::str
implement<A> trait Ord for struct String<A>
where A: Allocator // std::collections::string
implement trait Ord for struct Instant // std::time::clock
implement trait Ord for struct Duration // std::time::duration
Ordering
type enum Ordering {
Less;
Equal;
Greater;
}
implement enum Ordering {
reverse(&this) -> Ordering;
is_lt(&this) -> boolean;
is_gt(&this) -> boolean;
is_le(&this) -> boolean;
is_ge(&this) -> boolean;
}
The result of a comparison. reverse is useful for sorting by a reversed key without duplicating the comparator; the predicates is_lt, is_gt, is_le, and is_ge are what back the operators.
min, max, and clamp
function min<T>(a: T, b: T) -> T
where T: Ord;
function max<T>(a: T, b: T) -> T
where T: Ord;
function clamp<T>(value: T, lo: T, hi: T) -> T
where T: Ord;
Generic over any Ord. clamp panics if lo > hi, since that range is nonsense. math has the same three names dispatched over the numeric primitives at compile time, for code that does not want a trait bound.