@@ -7,7 +7,7 @@ can be awkward. Consider this code:
7
7
baz(bar(foo));
8
8
```
9
9
10
- We would read this left-to right, and so we see ‘baz bar foo’. But this isn’t the
10
+ We would read this left-to- right, and so we see ‘baz bar foo’. But this isn’t the
11
11
order that the functions would get called in, that’s inside-out: ‘foo bar baz’.
12
12
Wouldn’t it be nice if we could do this instead?
13
13
@@ -45,17 +45,17 @@ This will print `12.566371`.
45
45
46
46
47
47
48
- We’ve made a struct that represents a circle. We then write an ` impl ` block,
48
+ We’ve made a ` struct ` that represents a circle. We then write an ` impl ` block,
49
49
and inside it, define a method, ` area ` .
50
50
51
- Methods take a special first parameter, of which there are three variants:
51
+ Methods take a special first parameter, of which there are three variants:
52
52
` self ` , ` &self ` , and ` &mut self ` . You can think of this first parameter as
53
53
being the ` foo ` in ` foo.bar() ` . The three variants correspond to the three
54
54
kinds of things ` foo ` could be: ` self ` if it’s just a value on the stack,
55
55
` &self ` if it’s a reference, and ` &mut self ` if it’s a mutable reference.
56
56
Because we took the ` &self ` parameter to ` area ` , we can use it just like any
57
57
other parameter. Because we know it’s a ` Circle ` , we can access the ` radius `
58
- just like we would with any other struct.
58
+ just like we would with any other ` struct ` .
59
59
60
60
We should default to using ` &self ` , as you should prefer borrowing over taking
61
61
ownership, as well as taking immutable references over mutable ones. Here’s an
@@ -120,12 +120,12 @@ Check the return type:
120
120
``` rust
121
121
# struct Circle ;
122
122
# impl Circle {
123
- fn grow (& self ) -> Circle {
123
+ fn grow (& self , increment : f64 ) -> Circle {
124
124
# Circle } }
125
125
```
126
126
127
127
We just say we’re returning a ` Circle ` . With this method, we can grow a new
128
- circle to any arbitrary size.
128
+ ` Circle ` to any arbitrary size.
129
129
130
130
# Associated functions
131
131
@@ -161,7 +161,7 @@ methods’.
161
161
162
162
# Builder Pattern
163
163
164
- Let’s say that we want our users to be able to create Circles , but we will
164
+ Let’s say that we want our users to be able to create ` Circle ` s , but we will
165
165
allow them to only set the properties they care about. Otherwise, the ` x `
166
166
and ` y ` attributes will be ` 0.0 ` , and the ` radius ` will be ` 1.0 ` . Rust doesn’t
167
167
have method overloading, named arguments, or variable arguments. We employ
@@ -224,7 +224,7 @@ fn main() {
224
224
}
225
225
```
226
226
227
- What we’ve done here is make another struct, ` CircleBuilder ` . We’ve defined our
227
+ What we’ve done here is make another ` struct ` , ` CircleBuilder ` . We’ve defined our
228
228
builder methods on it. We’ve also defined our ` area() ` method on ` Circle ` . We
229
229
also made one more method on ` CircleBuilder ` : ` finalize() ` . This method creates
230
230
our final ` Circle ` from the builder. Now, we’ve used the type system to enforce
0 commit comments