Skip to content

Commit 775fe37

Browse files
authored
Rollup merge of #92953 - azdavis:azdavis-copy-example, r=dtolnay
Copy an example to PartialOrd as well In #88202 I added an example for deriving PartialOrd on enums, but only later did I realize that I actually put the example on Ord. This copies the example to PartialOrd as well, which is where I intended for it to be. We could also delete the example on Ord, but I see there's already some highly similar examples shared between Ord and PartialOrd, so I figured we could leave it. I also changed some type annotations in an example from `x : T` to the more common style (in Rust) of `x: T`.
2 parents 0aae1ec + bfe0a4e commit 775fe37

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

library/core/src/cmp.rs

+61-15
Original file line numberDiff line numberDiff line change
@@ -661,20 +661,37 @@ impl<T: Clone> Clone for Reverse<T> {
661661
///
662662
/// ## Derivable
663663
///
664-
/// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
665-
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering based on the top-to-bottom declaration order of the struct's members.
666-
/// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order.
667-
/// This means variants at the top are less than variants at the bottom.
668-
/// Here's an example:
664+
/// This trait can be used with `#[derive]`.
665+
///
666+
/// When `derive`d on structs, it will produce a
667+
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
668+
/// based on the top-to-bottom declaration order of the struct's members.
669+
///
670+
/// When `derive`d on enums, variants are ordered by their discriminants.
671+
/// By default, the discriminant is smallest for variants at the top, and
672+
/// largest for variants at the bottom. Here's an example:
669673
///
670674
/// ```
671-
/// #[derive(PartialEq, PartialOrd)]
672-
/// enum Size {
673-
/// Small,
674-
/// Large,
675+
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
676+
/// enum E {
677+
/// Top,
678+
/// Bottom,
675679
/// }
676680
///
677-
/// assert!(Size::Small < Size::Large);
681+
/// assert!(E::Top < E::Bottom);
682+
/// ```
683+
///
684+
/// However, manually setting the discriminants can override this default
685+
/// behavior:
686+
///
687+
/// ```
688+
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
689+
/// enum E {
690+
/// Top = 2,
691+
/// Bottom = 1,
692+
/// }
693+
///
694+
/// assert!(E::Bottom < E::Top);
678695
/// ```
679696
///
680697
/// ## Lexicographical comparison
@@ -895,9 +912,38 @@ impl PartialOrd for Ordering {
895912
///
896913
/// ## Derivable
897914
///
898-
/// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
899-
/// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
900-
/// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order.
915+
/// This trait can be used with `#[derive]`.
916+
///
917+
/// When `derive`d on structs, it will produce a
918+
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
919+
/// based on the top-to-bottom declaration order of the struct's members.
920+
///
921+
/// When `derive`d on enums, variants are ordered by their discriminants.
922+
/// By default, the discriminant is smallest for variants at the top, and
923+
/// largest for variants at the bottom. Here's an example:
924+
///
925+
/// ```
926+
/// #[derive(PartialEq, PartialOrd)]
927+
/// enum E {
928+
/// Top,
929+
/// Bottom,
930+
/// }
931+
///
932+
/// assert!(E::Top < E::Bottom);
933+
/// ```
934+
///
935+
/// However, manually setting the discriminants can override this default
936+
/// behavior:
937+
///
938+
/// ```
939+
/// #[derive(PartialEq, PartialOrd)]
940+
/// enum E {
941+
/// Top = 2,
942+
/// Bottom = 1,
943+
/// }
944+
///
945+
/// assert!(E::Bottom < E::Top);
946+
/// ```
901947
///
902948
/// ## How can I implement `PartialOrd`?
903949
///
@@ -970,8 +1016,8 @@ impl PartialOrd for Ordering {
9701016
/// # Examples
9711017
///
9721018
/// ```
973-
/// let x : u32 = 0;
974-
/// let y : u32 = 1;
1019+
/// let x: u32 = 0;
1020+
/// let y: u32 = 1;
9751021
///
9761022
/// assert_eq!(x < y, true);
9771023
/// assert_eq!(x.lt(&y), true);

0 commit comments

Comments
 (0)