-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add example using Self to reference #37386
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Manishearth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@bors r+ rollup thanks! |
📌 Commit d211eeb has been approved by |
@bors-servo r- doctest fail
Add the definition of |
@Manishearth Sorry about that, should have checked the Travis build. Will check the Travis build after this latest fix. |
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.
All good except the little typo.
@@ -3769,7 +3769,20 @@ impl Printable for String { | |||
|
|||
The notation `&self` is a shorthand for `self: &Self`. In this case, | |||
in the impl, `Self` refers to the value of type `String` that is the | |||
receiver for a call to the method `make_string`. | |||
receiver for a call to the method `make_string`. Thus, it could also |
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.
Please remove the extra whitespace before "Thus".
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 is still pending.
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.
@GuillaumeGomez Everything good now?
Once done, can you squash your commits please? If you don't know how to do it, you can take a look here. |
@GuillaumeGomez Thanks for the advice. The link you shared seems a little out of date (no more |
# } | ||
# | ||
impl Printable for String { | ||
fn make_string(&self) -> Self { |
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.
Why returning Self
whereas the trait method return String
? To be more clear: even if you implement the trait on String
, the method should still returns String
and not Self
for more clarity.
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.
Yeah, this may not be best example.
Edit: You're right, this is not a good example.
The purpose of the PR is that when I first ran in to Self
in the wild (not self
) I was confused how it worked since I hadn't seen something like that in another language. When I came to the docs, the examples used self
but not Self
directly. I wanted to get some example into the reference that actually used Self
directly, so that someone else new would looked it up would immediately see it used in an example.
Is there a better way to show an example of actually using Self
directly?
This is an example of what I ran into the wild and was looking for an example to help understand:
impl<I> From<AfterRenderArgs> for Event<I> {
fn from(args: AfterRenderArgs) -> Self {
Event::AfterRender(args)
}
}
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.
Yes, the From
trait seems like an excellent excellent to demonstrate 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.
Okay, let me know what you think of the latest update.
} | ||
``` | ||
|
||
The notation `Self` in the impl refers to the implementing type, `String`. In another |
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.
"implementing type: String
."
So except for the little typo, the example is now really great. |
Good catch, I think everything is fixed up now. Hey, and thanks for working through this with me. As someone fairly new to Rust, I appreciate it. |
I'm a reviewer, so that's part of my "job". Thanks a lot for your work! |
@bors: r+ rollup |
📌 Commit 434c314 has been approved by |
…Gomez Add example using Self to reference When I first came across `Self` I had a hard time finding references to it in the docs (and it's also been asked about on [StackOverflow](http://stackoverflow.com/questions/32304595/whats-the-difference-between-self-and-self). I hope this example provides someone who comes across it for the first time a little more help. If there is a better way to show an example actually using `Self`, I'm happy to modify this. It was just the simplest place to start I could see.
When I first came across
Self
I had a hard time finding references to it in the docs (and it's also been asked about on StackOverflow.I hope this example provides someone who comes across it for the first time a little more help. If there is a better way to show an example actually using
Self
, I'm happy to modify this. It was just the simplest place to start I could see.