1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-21 06:36:31 +02:00

Makes ownership of self more accurate

It’s entirely possible that a method gives ownership of self to another object, or return `self`. 
For example
```
    fn id(self) -> Self {
        return self;
    }
```
or more realistically 
```
    fn or(self, other) -> Self {
      if (self.isValid()) { return self;}
      return other;
    }
```
This commit is contained in:
Arthur Milchior 2022-12-23 00:16:42 -08:00 committed by GitHub
parent 264047445f
commit 3631117446
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,8 @@ are other possible receivers for a method:
* `&mut self`: borrows the object from the caller using a unique and mutable * `&mut self`: borrows the object from the caller using a unique and mutable
reference. The object can be used again afterwards. reference. The object can be used again afterwards.
* `self`: takes ownership of the object and moves it away from the caller. The * `self`: takes ownership of the object and moves it away from the caller. The
method becomes the owner of the object and will drop (deallocate) it at the method becomes the owner of the object. The object will be drop (deallocated)
end of the scope. when the method returns, unless it’s ownership is explicitly
transmitted.
* No receiver: this becomes a static method on the struct. Typically used to * No receiver: this becomes a static method on the struct. Typically used to
create constructors which are called `new` by convention. create constructors which are called `new` by convention.