-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add another PartialEq example #51760
Add another PartialEq example #51760
Conversation
src/libcore/cmp.rs
Outdated
@@ -95,6 +96,35 @@ use self::Ordering::*; | |||
/// assert!(b1 != b3); | |||
/// ``` | |||
/// | |||
/// ##How can I implement it to compare with another type? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space between ##
and How
and it
=> PartialEq
imo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not the issue. ;)
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
e27a8eb
to
e69fa24
Compare
Your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
e69fa24
to
59782be
Compare
ping @steveklabnik |
Ping from triage! @steveklabnik @rust-lang/docs , we haven't heard from you for a while, will youh ave time to look into this PR? |
Ping from triage, @rust-lang/docs anyone would want to review this PR? |
src/libcore/cmp.rs
Outdated
@@ -95,6 +96,35 @@ use self::Ordering::*; | |||
/// assert!(b1 != b3); | |||
/// ``` | |||
/// | |||
/// ## How can I implement it to compare with another type? | |||
/// | |||
/// This is pretty easy to do. Let's take back our previous code and modify it: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure i agree with using "pretty easy" here. It's a poor phrase to use in documentation. How about something like this:
The type you can compare with is controlled by
PartialEq
's type parameter. For example, let's tweak our previous code a bit:
Then you can follow up below the example with something like:
By changing
impl PartialEq for Book
toimpl PartialEq<BookFormat> for Book
, we've changed what type we can use on the right side of the==
operator. This lets us use it in theassert!
statements at the bottom.
It would also be cool if you added one more example, combining them to show off that the same type can ==
to different types. Maybe continue with:
You can even combine these implementations to compare against both
Book
andBookFormat
, like this:(code sample with both
PartialEq
impls and both sets of assertions)
59782be
to
b3f1f65
Compare
Took your sentences and added the new code! (which was a good idea!) |
src/libcore/cmp.rs
Outdated
/// we've changed what type we can use on the right side of the `==` operator. | ||
/// This lets us use it in the `assert!` statements at the bottom. | ||
/// | ||
/// Of course, you can implement `PartialEq` to have `==` operator on two |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of "of course" either. How about:
You can also combine these implementations to let the
==
operator work with two different types:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine by me! 😉
b3f1f65
to
f92b2bb
Compare
Updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for missing the first ping :(
src/libcore/cmp.rs
Outdated
@@ -95,6 +96,76 @@ use self::Ordering::*; | |||
/// assert!(b1 != b3); | |||
/// ``` | |||
/// | |||
/// ## How can I implement it to compare with another type? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads as awkward to me; can we get one last tweak?
/// ## How can I compare two different types?
src/libcore/cmp.rs
Outdated
/// For example, let's tweak our previous code a bit: | ||
/// | ||
/// ``` | ||
/// enum BookFormat { Paperback, Hardback, Ebook } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this how rustfmt
formats this code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the code above as is so I don't know?
/// impl PartialEq<BookFormat> for Book { | ||
/// fn eq(&self, other: &BookFormat) -> bool { | ||
/// match (&self.format, other) { | ||
/// (&BookFormat::Paperback, &BookFormat::Paperback) => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this code need the &
s? I'd expect them to not these days.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes the code more clear.
src/libcore/cmp.rs
Outdated
/// assert!(b1 != BookFormat::Ebook); | ||
/// ``` | ||
/// | ||
/// By changing impl `PartialEq` for `Book` to `impl PartialEq<BookFormat> for Book`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the `s should be around the whole "impl PartialEq for book" here
Ping from triage @GuillaumeGomez: Some changes have been requested to your PR. |
Oh right! I'll update soon. |
f92b2bb
to
663d6cd
Compare
Updated to @steveklabnik's comments. |
Triage: @steveklabnik This PR requires your review. |
Seems like I failed my rebase. I'll fix the conflicts. |
663d6cd
to
3995bff
Compare
Updated. |
Ping from triage! This PR needs a review, can @steveklabnik or someone else from @rust-lang/docs review this? |
Looks good to me! Let's get this merged. @bors r+ rollup |
📌 Commit 3995bff has been approved by |
…q-example, r=QuietMisdreavus Add another PartialEq example r? @steveklabnik
…q-example, r=QuietMisdreavus Add another PartialEq example r? @steveklabnik
…q-example, r=QuietMisdreavus Add another PartialEq example r? @steveklabnik
…q-example, r=QuietMisdreavus Add another PartialEq example r? @steveklabnik
These examples to not follow the rules the documentation states for Cc @gnzlbg |
…q-example, r=QuietMisdreavus Add another PartialEq example r? @steveklabnik
Rollup of 20 pull requests Successful merges: - #51760 (Add another PartialEq example) - #53113 (Add example for Cow) - #53129 (remove `let x = baz` which was obscuring the real error) - #53389 (document effect of join on memory ordering) - #53472 (Use FxHash{Map,Set} instead of the default Hash{Map,Set} everywhere in rustc.) - #53476 (Add partialeq implementation for TryFromIntError type) - #53513 (Force-inline `shallow_resolve` at its hottest call site.) - #53655 (set applicability) - #53702 (Fix stabilisation version for macro_vis_matcher.) - #53727 (Do not suggest dereferencing in macro) - #53732 (save-analysis: Differentiate foreign functions and statics.) - #53740 (add llvm-readobj to llvm-tools-preview) - #53743 (fix a typo: taget_env -> target_env) - #53747 (Rustdoc fixes) - #53753 (expand keep-stage --help text) - #53756 (Fix typo in comment) - #53768 (move file-extension based .gitignore down to src/) - #53785 (Fix a comment in src/libcore/slice/mod.rs) - #53786 (Replace usages of 'bad_style' with 'nonstandard_style'.) - #53806 (Fix UI issues on Implementations on Foreign types) Failed merges: r? @ghost
r? @steveklabnik