Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between self and Self?

Tags:

rust

People also ask

Is yourself and your self the same?

Yourself is correct and a reflexive pronoun in English. Yourselves can only be used when you are using “you” in the plural. Your self is used in another context. You need to bake the cake yourself.

What is the difference between self and other?

The binary of self and other is perhaps one of the most basic theories of human consciousness and identity, claiming, in short, that the existence of an other, a not-self, allows the possibility or recognition of a self. In other words: I see you. I do not control your body or hear your thoughts. You are separate.

What is the difference between self and body?

The needs of the body are physical in nature, whereas the needs of the self ('I') are not physical in nature - like trust, respect, happiness etc.

What is self in Swift?

In Swift, the self keyword refers to the object itself. It is used inside the class/structure to modify the properties of the object. You can assign initial values to properties in the init() method via self.


Self is the type of the current object. It may appear either in a trait or an impl, but appears most often in trait where it is a stand-in for whatever type will end up implementing the trait (which is unknown when defining the trait):

trait Clone {
    fn clone(&self) -> Self;
}

If I then implement Clone:

impl Clone for MyType {
    // I can use either the concrete type (known here)
    fn clone(&self) -> MyType;

    // Or I can use Self again, it's shorter after all!
    fn clone(&self) -> Self;
}

I could also use it in a regular impl if I am lazy (it's shorter!):

impl MySuperLongType {
    fn new(a: u32) -> Self { ... }
}

self is the name used in a trait or an impl for the first argument of a method. Using another name is possible, however there is a notable difference:

  • if using self, the function introduced is a method
  • if using any other name, the function introduced is an associated function

In Rust, there is no implicit this argument passed to a type's methods: you have to explicitly pass the "current object" as a method parameter. This would result in:

impl MyType {
    fn doit(this: &MyType, a: u32) { ... }
}

As we have seen, as a shorter form this could also be (still verbose):

impl MyType {
    fn doit(this: &Self, a: u32) { ... }
}

Which is actually what &self boils down to under the covers.

impl MyType {
    fn doit(&self, a: u32) { ... }
}

Thus the correspondence table:

self => self: Self
&self => self: &Self
&mut self => self: &mut Self

The way to invoke those functions change, however:

impl MyType {
    fn doit(&self, a: u32) {
        // ...
    }
    fn another(this: &Self, a: u32) {
        // ...
    }
}

fn main() {
    let m = MyType;

    // Both can be used as an associated function
    MyType::doit(&m, 1);
    MyType::another(&m, 2);

    // But only `doit` can be used in method position
    m.doit(3);     // OK: `m` is automatically borrowed
    m.another(4);  // ERROR: no method named `another`
}

self when used as first method argument, is a shorthand for self: Self. There are also &self, which is equivalent to self: &Self, and &mut self, which is equivalent to self: &mut Self.

Self in method arguments is syntactic sugar for the receiving type of the method (i.e. the type whose impl this method is in). This also allows for generic types without too much repetition.


Self refers to current type that implements the trait, self on the other hand refers to the instance.

Having self as the first parameter is how rust defines methods. It is just a convention that converts a function into a method, much like in python. Functionally, self is analogous to this in JavaScript.

For those who don't know the difference between a function and a method, methods are functions that are attached to an instance and invoked via that instance.