Skip to content

Commit cb2be32

Browse files
authored
Rollup merge of rust-lang#88202 - azdavis:master, r=jyn514
Add an example for deriving PartialOrd on enums For some reason, I always forget which variants are smaller and which are larger when you derive PartialOrd on an enum. And the wording in the current docs is not entirely clear to me. So, I often end up making a small enum, deriving PartialOrd on it, and then writing a `#[test]` with an assert that the top one is smaller than the bottom one (or the other way around) to figure out which way the deriving goes. So then I figured, it would be great if the standard library docs just had that example, so if I keep forgetting, at least I can figure it out quickly by looking at std's docs.
2 parents fbdff7f + 003a636 commit cb2be32

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

library/core/src/cmp.rs

+12
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,18 @@ impl<T: Clone> Clone for Reverse<T> {
660660
/// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
661661
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering based on the top-to-bottom declaration order of the struct's members.
662662
/// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order.
663+
/// This means variants at the top are less than variants at the bottom.
664+
/// Here's an example:
665+
///
666+
/// ```
667+
/// #[derive(PartialEq, PartialOrd)]
668+
/// enum Size {
669+
/// Small,
670+
/// Large,
671+
/// }
672+
///
673+
/// assert!(Size::Small < Size::Large);
674+
/// ```
663675
///
664676
/// ## Lexicographical comparison
665677
///

0 commit comments

Comments
 (0)