File tree 1 file changed +22
-1
lines changed
1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -50,7 +50,28 @@ You can think of this first parameter as being the `x` in `x.foo()`. The three
50
50
variants correspond to the three kinds of thing ` x ` could be: ` self ` if it's
51
51
just a value on the stack, ` &self ` if it's a reference, and ` &mut self ` if it's
52
52
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
+ ```
54
75
55
76
Finally, as you may remember, the value of the area of a circle is ` π*r² ` .
56
77
Because we took the ` &self ` parameter to ` area ` , we can use it just like any
You can’t perform that action at this time.
0 commit comments