Skip to content

Commit 10960d3

Browse files
committed
Rollup merge of #23188 - steveklabnik:gh18787, r=alexcrichton
Fixes #18787
2 parents b515b4e + 044b3bf commit 10960d3

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/doc/trpl/method-syntax.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,28 @@ You can think of this first parameter as being the `x` in `x.foo()`. The three
5050
variants correspond to the three kinds of thing `x` could be: `self` if it's
5151
just a value on the stack, `&self` if it's a reference, and `&mut self` if it's
5252
a mutable reference. We should default to using `&self`, as it's the most
53-
common.
53+
common. Here's an example of all three variants:
54+
55+
```rust
56+
struct Circle {
57+
x: f64,
58+
y: f64,
59+
radius: f64,
60+
}
61+
62+
impl Circle {
63+
fn reference(&self) {
64+
println!("taking self by reference!");
65+
}
66+
67+
fn mutable_reference(&mut self) {
68+
println!("taking self by mutable reference!");
69+
}
70+
71+
fn takes_ownership(self) {
72+
println!("taking ownership of self!");
73+
}
74+
```
5475

5576
Finally, as you may remember, the value of the area of a circle is `π*r²`.
5677
Because we took the `&self` parameter to `area`, we can use it just like any

0 commit comments

Comments
 (0)