How to Use std

:cmp for Ordering and Comparison

Use std::cmp::Ordering with .cmp() and match to compare values and handle less, greater, or equal cases.

Use std::cmp::Ordering with the .cmp() method to compare two values and handle the result with a match statement. This pattern determines if the first value is less than, greater than, or equal to the second.

use std::cmp::Ordering;

fn main() {
    let a = 5;
    let b = 10;

    match a.cmp(&b) {
        Ordering::Less => println!("{a} is less than {b}"),
        Ordering::Greater => println!("{a} is greater than {b}"),
        Ordering::Equal => println!("{a} is equal to {b}"),
    }
}