Compare integers
-
cmp
-
Less
-
Greater
-
Equal
-
Ordering
-
We can use the regular
<,>,==operators to compare any type of integers assuming the two sides are from the same type. -
The
cmpmethod returns a value from the Ordering enum.
fn main() {
let x = 1;
let y = 2;
let z = 1;
println!("{}", x < y);
println!("{:?}", x.cmp(&y));
println!("{}", y < x);
println!("{:?}", y.cmp(&x));
println!("{}", x < z);
println!("{:?}", x.cmp(&z));
println!();
let x: u8 = 1;
let y: u8 = 2;
println!("{}", x < y);
println!("{:?}", x.cmp(&y));
}
true
Less
false
Greater
false
Equal
true
Less