]> git.lizzy.rs Git - rust.git/commitdiff
Add an explicit "How can I implement `Eq`" doc section
authorCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Sat, 21 May 2016 17:05:15 +0000 (13:05 -0400)
committerCarol (Nichols || Goulding) <carol.nichols@gmail.com>
Mon, 23 May 2016 14:03:44 +0000 (10:03 -0400)
Building on the example in PartialEq.

src/libcore/cmp.rs

index f99358b09f4cb27c9a0f4a4fac499c62feac7035..d536d3ada3ff7dc77e9354bf360cb863b6a30450 100644 (file)
@@ -126,9 +126,32 @@ fn ne(&self, other: &Rhs) -> bool { !self.eq(other) }
 /// 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