Idiomatic Rust

1 min read Original article ↗

Similar to the Min-Max methods, Ordering, an enum from the std::cmp module, represents the outcome of comparing two values, offering Ordering::Less, Ordering::Equal, and Ordering::Greater. This approach enhances code clarity and consistency, replacing potentially complex boolean logic with explicit, structured comparisons. For instance, comparing 5 and 7 using int_a.cmp(&int_b) yields Ordering::Less, resulting in "Less" being printed, demonstrating how Ordering simplifies value comparison in Rust.

Instead of relying on boolean operators (<, >, ==), which can sometimes lead to complex conditional logic, Ordering provides a more explicit and readable way to represent comparisons. The match statement used with Ordering makes the code very clear about the possible outcomes of the comparison.