From: Carol (Nichols || Goulding) Date: Sat, 21 May 2016 17:05:15 +0000 (-0400) Subject: Add an explicit "How can I implement `Eq`" doc section X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=54d2ef0e8ef8b03b2a0548512905484d3f094a99;p=rust.git Add an explicit "How can I implement `Eq`" doc section Building on the example in PartialEq. --- 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 @@ 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 { // FIXME #13101: this method is used solely by #[deriving] to