- cmp
- Less
- Greater
- Equal
- Ordering
Compare integers
- We can use the regular <, >, == operators to compare any type of integers assuming the two sides are from the same type.
- The cmp method returns a value from the Ordering enum.
examples/numbers/compare-integers/src/main.rs
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