@@ -51,7 +51,8 @@ You can think of this first parameter as being the `x` in `x.foo()`. The three
51
51
variants correspond to the three kinds of thing ` x ` could be: ` self ` if it's
52
52
just a value on the stack, ` &self ` if it's a reference, and ` &mut self ` if it's
53
53
a mutable reference. We should default to using ` &self ` , as it's the most
54
- common. Here's an example of all three variants:
54
+ common, as Rustaceans prefer borrowing over taking ownership, and references
55
+ over mutable references. Here's an example of all three variants:
55
56
56
57
``` rust
57
58
struct Circle {
@@ -100,16 +101,16 @@ impl Circle {
100
101
std::f64::consts::PI * (self.radius * self.radius)
101
102
}
102
103
103
- fn grow(&self) -> Circle {
104
- Circle { x: self.x, y: self.y, radius: ( self.radius * 10.0) }
104
+ fn grow(&self, increment: f64 ) -> Circle {
105
+ Circle { x: self.x, y: self.y, radius: self.radius + increment }
105
106
}
106
107
}
107
108
108
109
fn main() {
109
110
let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
110
111
println!("{}", c.area());
111
112
112
- let d = c.grow().area();
113
+ let d = c.grow(2.0 ).area();
113
114
println!("{}", d);
114
115
}
115
116
```
@@ -124,7 +125,7 @@ fn grow(&self) -> Circle {
124
125
```
125
126
126
127
We just say we're returning a ` Circle ` . With this method, we can grow a new
127
- circle with an area that's 100 times larger than the old one .
128
+ circle to any arbitrary size .
128
129
129
130
## Static methods
130
131
0 commit comments