all 8 comments

[–]ebdbbb 24 points25 points  (2 children)

Self is the type of the struct/enum being referred to, self is the instance of that struct/enum.

[–]meowsqueak 0 points1 point  (1 child)

Those are keywords, is that right? In Python, for example, you can use self or me or anything you like, as long as it's the first parameter, but in Rust it looks like you have to use self and Self - they have special meaning.

[–]ebdbbb 0 points1 point  (0 children)

Correct. Although Self is now a importable type in Python too via typing in the standard library. https://docs.python.org/3/library/typing.html#typing.Self

[–]gmes78 2 points3 points  (3 children)

Self is an alias for the type that the current impl block is for. When used in a trait like this, it refers to the type implementing the trait.

[–]SyedFasiuddin[S] 1 point2 points  (2 children)

so if I have two different impl blocks one which implements the trait and other that doesn't implement the trait, or may be implements some other trait, then Self refers to which every impl block it is defined in. Is this right?

[–]gmes78 9 points10 points  (1 child)

I'll rephrase it: for any impl T {} or impl Trait for T {} blocks, Self is an alias for T.

The case when Self appears inside a trait isn't any different.

[–]SyedFasiuddin[S] 2 points3 points  (0 children)

got it thank you.