From 33921c9e6c30e07bbc1a6187ac364429a84d04bf Mon Sep 17 00:00:00 2001 From: polazarus Date: Wed, 12 Feb 2020 21:18:41 +0100 Subject: [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). --- rust.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rust.html.markdown b/rust.html.markdown index 413939bf..83682c01 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -176,7 +176,13 @@ fn main() { impl Foo { // 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 } } -- cgit v1.2.3