Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use self, &self, &mut self in methods?

Tags:

rust

What is the difference between a method taking self and a method taking &self or even &mut self?

E.g.

impl SomeStruct {
    fn example1(self) { }
    fn example2(&self) { }
    fn example3(&mut self) { }
}

Say I want to implement a method that pretty prints the struct to stdout, should I take &self? I guess self also works? I'm not sure when to use what.

like image 893
Aart Stuurman Avatar asked Nov 24 '19 13:11

Aart Stuurman


1 Answers

From the example in the book (just split onto three separate paragraphs and marked to maybe make it clearer):

&self: We’ve chosen &self here for the same reason we used &Rectangle in the function version: we don’t want to take ownership, and we just want to read the data in the struct, not write to it.

&mut self: If we wanted to change the instance that we’ve called the method on as part of what the method does, we’d use &mut self as the first parameter.

self: Having a method that takes ownership of the instance by using just self as the first parameter is rare; this technique is usually used when the method transforms self into something else and you want to prevent the caller from using the original instance after the transformation.

Or in other words: the difference is exactly the same as SomeStruct, &SomeStruct, and &mut SomeStruct.

Say I want to implement a method that pretty prints the struct to stdout, should I take &self? I guess self also works?

As you can see, this is exactly a case for &self. If you use self (or &mut self) the method will likely still compile, but it can only be used in more restricted situations.

like image 53
Alexey Romanov Avatar answered Sep 21 '22 06:09

Alexey Romanov