[−][src]Keyword Self
The implementing type within a trait
or impl
block, or the current type within a type
definition.
Within a type definition:
struct Node { elem: i32, // `Self` is a `Node` here. next: Option<Box<Self>>, }Run
In an impl
block:
struct Foo(i32); impl Foo { fn new() -> Self { Self(0) } } assert_eq!(Foo::new().0, Foo(0).0);Run
Generic parameters are implicit with Self
:
struct Wrap<T> { elem: T, } impl<T> Wrap<T> { fn new(elem: T) -> Self { Self { elem } } }Run
In a trait
definition and related impl
block:
trait Example { fn example() -> Self; } struct Foo(i32); impl Example for Foo { fn example() -> Self { Self(42) } } assert_eq!(Foo::example().0, Foo(42).0);Run