cmp
import std::core::cmp; — in the prelude · source
Types
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.
Methods
equals(&this, other: &This) -> boolean;
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.
Methods
compare(&this, other: &This) -> Ordering;
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.
Variants
| Variant |
|---|
Less |
Equal |
Greater |
Methods
reverse(&this) -> Ordering;
reverse(&this) -> Ordering;Flip the direction of the comparison. Useful when sorting by a reversed key without duplicating the comparator.
is_lt(&this) -> boolean;
is_lt(&this) -> boolean;Ordering predicates backing the relational operators on Ord types. The compiler rewrites a < b into a.compare(&b).is_lt() (and >, <=, >= into is_gt/is_le/is_ge), so these keep the operator semantics in one place and let Ord-only types get < > <= >= for free once they implement compare.
is_gt(&this) -> boolean;
is_le(&this) -> boolean;
is_ge(&this) -> boolean;
Free Functions
min, max, and clamp
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.
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;
function clamp<T>(value: T, lo: T, hi: T) -> T where T: Ord;Constrain value to [lo, hi]. Panics if lo > hi; the caller gave a nonsense range.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
Eq::equals(&this, other: &This) -> boolean&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) other: &ThisThisThe implementing type, inside a trait or implement body. other: &This in a trait method is a reference to whichever type implements it; This::Item names an associated type.
type trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.
type trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
Ord::compare(&this, other: &This) -> Orderingtype enum Ordering {
Less;
Equal;
Greater;
}type enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum Ordering {
Less;
Equal;
Greater;
}Ordering::LessOrdering::EqualOrdering::Greaterimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement enum Name { ... }Names the enum an inherent block extends - the only way to give an enum methods.
language referenceOrdering::reverse(&this) -> OrderingFlip the direction of the comparison. Useful when sorting by a reversed key without duplicating the comparator.
Ordering::is_lt(&this) -> booleanOrdering predicates backing the relational operators on Ord types. The compiler rewrites a < b into a.compare(&b).is_lt() (and >, <=, >= into is_gt/is_le/is_ge), so these keep the operator semantics in one place and let Ord-only types get < > <= >= for free once they implement compare.
Ordering::is_gt(&this) -> booleanOrdering::is_le(&this) -> booleanOrdering::is_ge(&this) -> booleanimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
Eq::equals(&this, other: &This) -> booleantype struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraytype trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.
falseThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referenceprimitive usizePointer-width unsigned integer; the natural type for sizes and indices.
primitive typeprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
type parameter Twhere T: Eq
type parameter Awhere A: Allocator
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatortype parameter Awhere A: Eq
type parameter Bwhere B: Eq
type struct Pair<A, B> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
std::collections::pairtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype parameter Ewhere E: Eq
type enum RandomError {
EntropyUnavailable;
EmptyRange;
InvalidWeights;
}What went wrong in a fallible random operation.
std::random::errortype struct Instant {
secs: i64;
nanos: i64;
}A measurement of the monotonic clock. Opaque: the fields are the raw timespec, meaningful only relative to another Instant. POD.
type struct Duration {
secs: u64;
nanos: u32;
}A length of time: whole secs plus a sub-second nanos remainder (0 <= nanos < 1_000_000_000). POD; copy-by-value, no drop.
Ord::compare(&this, other: &This) -> OrderingOrdering::reverse(&this) -> OrderingFlip the direction of the comparison. Useful when sorting by a reversed key without duplicating the comparator.
std::core::cmpOrdering::is_lt(&this) -> booleanOrdering predicates backing the relational operators on Ord types. The compiler rewrites a < b into a.compare(&b).is_lt() (and >, <=, >= into is_gt/is_le/is_ge), so these keep the operator semantics in one place and let Ord-only types get < > <= >= for free once they implement compare.
Ordering::is_gt(&this) -> booleanOrdering::is_le(&this) -> booleanOrdering::is_ge(&this) -> booleanOrdering::LessOrdering::EqualOrdering::Greaterfunction clamp<T>(value: T, lo: T, hi: T) -> T
where T: OrdConstrain value to [lo, hi]. Panics if lo > hi; the caller gave a nonsense range.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function min<T>(a: T, b: T) -> T
where T: Ordtype parameter Twhere T: Ord
(parameter) a: T(parameter) b: Tfunction max<T>(a: T, b: T) -> T
where T: Ordfunction clamp<T>(value: T, lo: T, hi: T) -> T
where T: OrdConstrain value to [lo, hi]. Panics if lo > hi; the caller gave a nonsense range.
(parameter) value: T(parameter) lo: T(parameter) hi: T