Self vs self in Rust
In Rust, Self
and self
have different meanings and uses:
Self
refers to the type that is implementing a trait or a method. It is used in type definitions and associated functions.self
refers to the instance of the type that is implementing a method. It is used in method signatures and method bodies.
Here is an example to illustrate the difference:
#![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 } } }
In this example:
Self
is used in thenew
function to refer to the typeMyStruct
.self
is used in theconsume
method to refer to the instance ofMyStruct
.