Skip to content

Commit ef119bc

Browse files
authored
Update receiver.md (#229)
The key takeaway is mutability of receivers and the rules that come with it. It might be a repetition of borrow checker rules, but it is important to know they apply to `self` as to any other variable or argument.
1 parent c109dab commit ef119bc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/methods/receiver.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ are other possible receivers for a method:
1111
method becomes the owner of the object. The object will be dropped (deallocated)
1212
when the method returns, unless its ownership is explicitly
1313
transmitted.
14+
* `mut self`: same as above, but while the method owns the object, it can
15+
mutate it too. Complete ownership does not automatically mean mutability.
1416
* No receiver: this becomes a static method on the struct. Typically used to
1517
create constructors which are called `new` by convention.
1618

1719
Beyond variants on `self`, there are also
1820
[special wrapper types](https://doc.rust-lang.org/reference/special-types-and-traits.html)
1921
allowed to be receiver types, such as `Box<Self>`.
22+
23+
<details>
24+
25+
Consider emphasizing on "shared and immutable" and "unique and mutable". These constraints always come
26+
together in Rust due to borrow checker rules, and `self` is no exception. It won't be possible to
27+
reference a struct from multiple locations and call a mutating (`&mut self`) method on it.
28+
29+
</details>

0 commit comments

Comments
 (0)