Self vs self in Rust
In Rust, Self and self have different meanings and uses:
Selfrefers to the type that is implementing a trait or a method. It is used in type definitions and associated functions.selfrefers to the instance of the type that is implementing a method. It is used in method signatures and method bodies.
#![allow(unused)] fn main() { struct MyStruct; impl MyStruct { // Associated function, uses `Self` fn new() -> Self { MyStruct } // Method, uses `self` fn consume(self) { // consume the instance } } }
Selfis used in thenewfunction to refer to the typeMyStruct.selfis used in theconsumemethod to refer to the instance ofMyStruct.