diff options
| author | Carol (Nichols || Goulding) <carol.nichols@gmail.com> | 2016-05-21 13:05:15 -0400 |
|---|---|---|
| committer | Carol (Nichols || Goulding) <carol.nichols@gmail.com> | 2016-05-23 10:03:44 -0400 |
| commit | 54d2ef0e8ef8b03b2a0548512905484d3f094a99 (patch) | |
| tree | c43c89a2ce3f0388b850cc123aadcf3db0542d17 /src/libcore/cmp.rs | |
| parent | e831c72a299a9f3320f30b4ff22d100092d56640 (diff) | |
| download | rust-54d2ef0e8ef8b03b2a0548512905484d3f094a99.tar.gz rust-54d2ef0e8ef8b03b2a0548512905484d3f094a99.zip | |
Add an explicit "How can I implement `Eq`" doc section
Building on the example in PartialEq.
Diffstat (limited to 'src/libcore/cmp.rs')
| -rw-r--r-- | src/libcore/cmp.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index f99358b09f4..d536d3ada3f 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -126,9 +126,32 @@ pub trait PartialEq<Rhs: ?Sized = Self> { /// This property cannot be checked by the compiler, and therefore `Eq` implies /// `PartialEq`, and has no extra methods. /// +/// ## Derivable +/// /// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has /// no extra methods, it is only informing the compiler that this is an -/// equivalence relation rather than a partial equivalence relation. +/// equivalence relation rather than a partial equivalence relation. Note that +/// the `derive` strategy requires all fields are `PartialEq`, which isn't +/// always desired. +/// +/// ## How can I implement `Eq`? +/// +/// If you cannot use the `derive` strategy, specify that your type implements +/// `Eq`, which has no methods: +/// +/// ``` +/// enum BookFormat { Paperback, Hardback, Ebook } +/// struct Book { +/// isbn: i32, +/// format: BookFormat, +/// } +/// impl PartialEq for Book { +/// fn eq(&self, other: &Self) -> bool { +/// self.isbn == other.isbn +/// } +/// } +/// impl Eq for Book {} +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub trait Eq: PartialEq<Self> { // FIXME #13101: this method is used solely by #[deriving] to |
