Use From trait as an example usage of Self.
This commit is contained in:
parent
ac468b67bf
commit
434c314405
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user