Skip to content

Commit 161f262

Browse files
authored
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.
2 parents 3fc8304 + 434c314 commit 161f262

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/doc/reference.md

+15
Original file line numberDiff line numberDiff line change
@@ -3755,6 +3755,21 @@ The special type `Self` has a meaning within traits and impls. In a trait defini
37553755
to an implicit type parameter representing the "implementing" type. In an impl,
37563756
it is an alias for the implementing type. For example, in:
37573757

3758+
```
3759+
pub trait From<T> {
3760+
fn from(T) -> Self;
3761+
}
3762+
3763+
impl From<i32> for String {
3764+
fn from(x: i32) -> Self {
3765+
x.to_string()
3766+
}
3767+
}
3768+
```
3769+
3770+
The notation `Self` in the impl refers to the implementing type: `String`. In another
3771+
example:
3772+
37583773
```
37593774
trait Printable {
37603775
fn make_string(&self) -> String;

0 commit comments

Comments
 (0)