summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--rust.html.markdown10
1 files changed, 8 insertions, 2 deletions
diff --git a/rust.html.markdown b/rust.html.markdown
index 413939bf..e835de12 100644
--- a/rust.html.markdown
+++ b/rust.html.markdown
@@ -176,13 +176,19 @@ 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
}
}
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) //