1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-01 12:30:48 +02: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:
polazarus
2020-02-12 21:18:41 +01:00
committed by GitHub
parent 76db052c9e
commit 33921c9e6c

View File

@@ -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
}
}