Skip to content

Commit

Permalink
Auto merge of rust-lang#37386 - johnthagen:Self-reference-example, r=…
Browse files Browse the repository at this point in the history
…GuillaumeGomez

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.
  • Loading branch information
bors authored Nov 6, 2016
2 parents 3fc8304 + 434c314 commit 161f262
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3755,6 +3755,21 @@ The special type `Self` has a meaning within traits and impls. In a trait defini
to an implicit type parameter representing the "implementing" type. In an impl,
it is an alias for the implementing type. For example, in:

```
pub trait From<T> {
fn from(T) -> Self;
}
impl From<i32> for String {
fn from(x: i32) -> Self {
x.to_string()
}
}
```

The notation `Self` in the impl refers to the implementing type: `String`. In another
example:

```
trait Printable {
fn make_string(&self) -> String;
Expand Down

0 comments on commit 161f262

Please sign in to comment.