keyword
Rust Self: name the implementing type
Uppercase `Self` refers to the type being implemented in an impl, or the eventual implementing type inside a trait. It keeps methods from repeating the full type name. Expected output (not executed): 0 Why: Within this impl, Self stands for Counter. Common mistake: Do not confuse the type `Self` with the receiver `self`.
Formal Syntax & Structure
rust syntaxrust
fn new() -> Self { Self { field: value } }Working Example
Example Snippetrust
struct Counter { count: u32 }impl Counter { fn new() -> Self { Self { count: 0 } }}
fn main() { println!("{}", Counter::new().count);}