mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
[Rust] Change misleading method and add two other methods
The `get_bar` method consumes `self`. The name is misleading and does not respect the language naming convention. This PR renames it to `into_bar` and provides `bar` (a getter) and `bar_mut` (to get a mutable reference).
This commit is contained in:
parent
76db052c9e
commit
33921c9e6c
@ -176,7 +176,13 @@ fn main() {
|
||||
|
||||
impl<T> Foo<T> {
|
||||
// Methods take an explicit `self` parameter
|
||||
fn get_bar(self) -> T {
|
||||
fn bar(&self) -> &T { // self is borrowed
|
||||
&self.bar
|
||||
}
|
||||
fn bar_mut(&mut self) -> &mut T { // self is mutably borrowed
|
||||
&mut self.bar
|
||||
}
|
||||
fn into_bar(self) -> T { // here self is consumed
|
||||
self.bar
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user