Ord
trait Ord<T: Lt + Le + Gt + Ge + Eq>
Source: compiler/src/builtin_docs/prelude/ops.hy The ordering bundle: Lt, Le, Gt and Ge, with Eq.
Ord has no methods of its own. A bound T: Ord allows <, <=, >,
>=, == and != on T, and range bounds (a..b) must be Ord.
Eq is a superclass, so a type that is Ord is Eq too: an impl Ord
or #[derive(Ord)] needs an Eq instance (#[derive(Eq, Ord)]). int, float,
byte and string are ordered (a string by its UTF-8 bytes, so
"B" < "a" and a prefix sorts first); bool is not. A string is not
a range bound. #[derive(Ord)] orders an enum or class by its fields in
declaration order, and a scalar-backed enum (#[repr(int)],
#[repr(string)], ..) by its backing value.
A tuple whose elements are all Ord is Ord too, ordered
lexicographically: the first element that differs decides
((1, "b") < (2, "a")).
Examples#
use tests::assert;
fn smaller<T: Ord>(T a, T b) -> T {
if a < b {
return a;
}
return b;
}
fn main() {
assert!(smaller(3, 2) == 2);
assert!(smaller(1.5, 2.5) == 1.5);
assert!(smaller("pear", "fig") == "fig");
assert!(smaller((1, "b"), (1, "a")) == (1, "a"));
}