diff options
| author | Corey Farwell <coreyf@rwell.org> | 2019-01-12 11:17:49 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-12 11:17:49 -0500 |
| commit | 1445a065ded1bc7e73e7a358f9bc971e908e9181 (patch) | |
| tree | a5a5b2cff7283afa7a6d887246b7974fb3dcc30e /src/libcore | |
| parent | 423a5bb5c437c2ce81d4150490d0cccfdbc33a4c (diff) | |
| download | rust-1445a065ded1bc7e73e7a358f9bc971e908e9181.tar.gz rust-1445a065ded1bc7e73e7a358f9bc971e908e9181.zip | |
bring back the example i removed, also add symmetry and simplify impl
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cmp.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 72ff9cca9fd..db72b7bb9d2 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -132,6 +132,48 @@ use self::Ordering::*; /// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`, /// we allow `BookFormat`s to be compared with `Book`s. /// +/// You can also combine these implementations to let the `==` operator work with +/// two different types: +/// +/// ``` +/// #[derive(PartialEq)] +/// enum BookFormat { +/// Paperback, +/// Hardback, +/// Ebook, +/// } +/// +/// struct Book { +/// isbn: i32, +/// format: BookFormat, +/// } +/// +/// impl PartialEq<BookFormat> for Book { +/// fn eq(&self, other: &BookFormat) -> bool { +/// self.format == *other +/// } +/// } +/// +/// impl PartialEq<Book> for BookFormat { +/// fn eq(&self, other: &Book) -> bool { +/// *self == other.format +/// } +/// } +/// +/// impl PartialEq for Book { +/// fn eq(&self, other: &Book) -> bool { +/// self.isbn == other.isbn +/// } +/// } +/// +/// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; +/// let b2 = Book { isbn: 3, format: BookFormat::Ebook }; +/// +/// assert!(b1 == BookFormat::Paperback); +/// assert!(b1 != BookFormat::Ebook); +/// assert!(b1 == b2); +/// ``` +/// /// # Examples /// /// ``` |
