about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2021-09-03 13:30:46 +0200
committerGitHub <noreply@github.com>2021-09-03 13:30:46 +0200
commitcb2be32dbd518badcecb5c43cfc30e3db857e5ef (patch)
tree2b0f933dded6001d26203ae336cb60b051df583d
parentfbdff7fae941bce21fd3047f87777c84b866850e (diff)
parent003a636e760d50f4e1e96101f8e9994ac3758135 (diff)
downloadrust-cb2be32dbd518badcecb5c43cfc30e3db857e5ef.tar.gz
rust-cb2be32dbd518badcecb5c43cfc30e3db857e5ef.zip
Rollup merge of #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.
-rw-r--r--library/core/src/cmp.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs
index 79610bb409d..4e82b655394 100644
--- a/library/core/src/cmp.rs
+++ b/library/core/src/cmp.rs
@@ -660,6 +660,18 @@ impl<T: Clone> Clone for Reverse<T> {
 /// 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:
+///
+/// ```
+/// #[derive(PartialEq, PartialOrd)]
+/// enum Size {
+///     Small,
+///     Large,
+/// }
+///
+/// assert!(Size::Small < Size::Large);
+/// ```
 ///
 /// ## Lexicographical comparison
 ///