diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2015-03-09 00:11:48 +0530 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2015-03-09 00:11:48 +0530 |
| commit | 10960d3043ac24b399084641f63210ed988dd2a3 (patch) | |
| tree | 92c132380f62f09205176af5d03a92880b964b4f | |
| parent | b515b4e28a47338b4c63e9ef2c393abe58dc9aaf (diff) | |
| parent | 044b3bf2d74de0b265e76fa5d0c735cdb15df690 (diff) | |
| download | rust-10960d3043ac24b399084641f63210ed988dd2a3.tar.gz rust-10960d3043ac24b399084641f63210ed988dd2a3.zip | |
Rollup merge of #23188 - steveklabnik:gh18787, r=alexcrichton
Fixes #18787
| -rw-r--r-- | src/doc/trpl/method-syntax.md | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/doc/trpl/method-syntax.md b/src/doc/trpl/method-syntax.md index 64d540582a3..b814ff16abb 100644 --- a/src/doc/trpl/method-syntax.md +++ b/src/doc/trpl/method-syntax.md @@ -50,7 +50,28 @@ You can think of this first parameter as being the `x` in `x.foo()`. The three variants correspond to the three kinds of thing `x` could be: `self` if it's just a value on the stack, `&self` if it's a reference, and `&mut self` if it's a mutable reference. We should default to using `&self`, as it's the most -common. +common. Here's an example of all three variants: + +```rust +struct Circle { + x: f64, + y: f64, + radius: f64, +} + +impl Circle { + fn reference(&self) { + println!("taking self by reference!"); + } + + fn mutable_reference(&mut self) { + println!("taking self by mutable reference!"); + } + + fn takes_ownership(self) { + println!("taking ownership of self!"); + } +``` Finally, as you may remember, the value of the area of a circle is `π*r²`. Because we took the `&self` parameter to `area`, we can use it just like any |
