diff options
| author | bors <bors@rust-lang.org> | 2016-11-06 02:28:58 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-11-06 02:28:58 -0800 |
| commit | 161f2623bd078017dfc9a06bd3417b36f6a58de9 (patch) | |
| tree | 59c03b437d099ef5ee025cea3117bd9e3a73348d | |
| parent | 3fc8304fd92720c688f3d6ac30b3a728d15f7a33 (diff) | |
| parent | 434c3144050859359d00ab6b9e21151093b99db8 (diff) | |
| download | rust-161f2623bd078017dfc9a06bd3417b36f6a58de9.tar.gz rust-161f2623bd078017dfc9a06bd3417b36f6a58de9.zip | |
Auto merge of #37386 - johnthagen:Self-reference-example, r=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.
| -rw-r--r-- | src/doc/reference.md | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index 4838ecd2d42..5c372fb7816 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -3756,6 +3756,21 @@ 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; } |
