From ef119bc0d321e479da3fa1e2c7dbe8d2c04d628a Mon Sep 17 00:00:00 2001 From: Igor Petruk Date: Mon, 23 Jan 2023 11:39:38 +0000 Subject: [PATCH] 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. --- src/methods/receiver.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/methods/receiver.md b/src/methods/receiver.md index 4c3f7e51..efe5997a 100644 --- a/src/methods/receiver.md +++ b/src/methods/receiver.md @@ -11,9 +11,19 @@ are other possible receivers for a method: method becomes the owner of the object. The object will be dropped (deallocated) when the method returns, unless its ownership is explicitly transmitted. +* `mut self`: same as above, but while the method owns the object, it can + mutate it too. Complete ownership does not automatically mean mutability. * No receiver: this becomes a static method on the struct. Typically used to create constructors which are called `new` by convention. Beyond variants on `self`, there are also [special wrapper types](https://doc.rust-lang.org/reference/special-types-and-traits.html) allowed to be receiver types, such as `Box`. + +
+ +Consider emphasizing on "shared and immutable" and "unique and mutable". These constraints always come +together in Rust due to borrow checker rules, and `self` is no exception. It won't be possible to +reference a struct from multiple locations and call a mutating (`&mut self`) method on it. + +