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 From 602dbf5267ff15ab137bee2dbedad7f500d039a2 Mon Sep 17 00:00:00 2001 From: polazarus Date: Wed, 12 Feb 2020 21:25:39 +0100 Subject: Change call --- rust.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust.html.markdown b/rust.html.markdown index 83682c01..e835de12 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -188,7 +188,7 @@ fn main() { } let a_foo = Foo { bar: 1 }; - println!("{}", a_foo.get_bar()); // 1 + println!("{}", a_foo.bar()); // 1 // Traits (known as interfaces or typeclasses in other languages) // -- cgit v1.2.3