Skip to content

Commit

Permalink
Rollup merge of #92953 - azdavis:azdavis-copy-example, r=dtolnay
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
matthiaskrgr authored Jan 17, 2022
2 parents 0aae1ec + bfe0a4e commit 775fe37
Showing 1 changed file with 61 additions and 15 deletions.
76 changes: 61 additions & 15 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,20 +661,37 @@ impl<T: Clone> Clone for Reverse<T> {
///
/// ## Derivable
///
/// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering based on the top-to-bottom declaration order of the struct's members.
/// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order.
/// This means variants at the top are less than variants at the bottom.
/// Here's an example:
/// This trait can be used with `#[derive]`.
///
/// When `derive`d on structs, it will produce a
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
/// based on the top-to-bottom declaration order of the struct's members.
///
/// When `derive`d on enums, variants are ordered by their discriminants.
/// By default, the discriminant is smallest for variants at the top, and
/// largest for variants at the bottom. Here's an example:
///
/// ```
/// #[derive(PartialEq, PartialOrd)]
/// enum Size {
/// Small,
/// Large,
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
/// enum E {
/// Top,
/// Bottom,
/// }
///
/// assert!(Size::Small < Size::Large);
/// assert!(E::Top < E::Bottom);
/// ```
///
/// However, manually setting the discriminants can override this default
/// behavior:
///
/// ```
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
/// enum E {
/// Top = 2,
/// Bottom = 1,
/// }
///
/// assert!(E::Bottom < E::Top);
/// ```
///
/// ## Lexicographical comparison
Expand Down Expand Up @@ -895,9 +912,38 @@ impl PartialOrd for Ordering {
///
/// ## Derivable
///
/// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a
/// lexicographic ordering based on the top-to-bottom declaration order of the struct's members.
/// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order.
/// This trait can be used with `#[derive]`.
///
/// When `derive`d on structs, it will produce a
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
/// based on the top-to-bottom declaration order of the struct's members.
///
/// When `derive`d on enums, variants are ordered by their discriminants.
/// By default, the discriminant is smallest for variants at the top, and
/// largest for variants at the bottom. Here's an example:
///
/// ```
/// #[derive(PartialEq, PartialOrd)]
/// enum E {
/// Top,
/// Bottom,
/// }
///
/// assert!(E::Top < E::Bottom);
/// ```
///
/// However, manually setting the discriminants can override this default
/// behavior:
///
/// ```
/// #[derive(PartialEq, PartialOrd)]
/// enum E {
/// Top = 2,
/// Bottom = 1,
/// }
///
/// assert!(E::Bottom < E::Top);
/// ```
///
/// ## How can I implement `PartialOrd`?
///
Expand Down Expand Up @@ -970,8 +1016,8 @@ impl PartialOrd for Ordering {
/// # Examples
///
/// ```
/// let x : u32 = 0;
/// let y : u32 = 1;
/// let x: u32 = 0;
/// let y: u32 = 1;
///
/// assert_eq!(x < y, true);
/// assert_eq!(x.lt(&y), true);
Expand Down

0 comments on commit 775fe37

Please sign in to comment.