Skip to content

Commit 54d2ef0

Browse files
committed
Add an explicit "How can I implement Eq" doc section
Building on the example in PartialEq.
1 parent e831c72 commit 54d2ef0

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/libcore/cmp.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,32 @@ pub trait PartialEq<Rhs: ?Sized = Self> {
126126
/// This property cannot be checked by the compiler, and therefore `Eq` implies
127127
/// `PartialEq`, and has no extra methods.
128128
///
129+
/// ## Derivable
130+
///
129131
/// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has
130132
/// no extra methods, it is only informing the compiler that this is an
131-
/// equivalence relation rather than a partial equivalence relation.
133+
/// equivalence relation rather than a partial equivalence relation. Note that
134+
/// the `derive` strategy requires all fields are `PartialEq`, which isn't
135+
/// always desired.
136+
///
137+
/// ## How can I implement `Eq`?
138+
///
139+
/// If you cannot use the `derive` strategy, specify that your type implements
140+
/// `Eq`, which has no methods:
141+
///
142+
/// ```
143+
/// enum BookFormat { Paperback, Hardback, Ebook }
144+
/// struct Book {
145+
/// isbn: i32,
146+
/// format: BookFormat,
147+
/// }
148+
/// impl PartialEq for Book {
149+
/// fn eq(&self, other: &Self) -> bool {
150+
/// self.isbn == other.isbn
151+
/// }
152+
/// }
153+
/// impl Eq for Book {}
154+
/// ```
132155
#[stable(feature = "rust1", since = "1.0.0")]
133156
pub trait Eq: PartialEq<Self> {
134157
// FIXME #13101: this method is used solely by #[deriving] to

0 commit comments

Comments
 (0)