3
3
Functions are great, but if you want to call a bunch of them on some data, it
4
4
can be awkward. Consider this code:
5
5
6
- ``` { rust,ignore}
6
+ ``` rust,ignore
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
11
- order that the functions would get called in, that' s inside-out: " foo bar baz."
12
- Wouldn' t it be nice if we could do this instead?
10
+ We would read this left-to right, and so we see ‘ baz bar foo’. But this isn’ t the
11
+ order that the functions would get called in, that’ s inside-out: ‘ foo bar baz’.
12
+ Wouldn’ t it be nice if we could do this instead?
13
13
14
- ``` { rust,ignore}
14
+ ``` rust,ignore
15
15
foo.bar().baz();
16
16
```
17
17
18
18
Luckily, as you may have guessed with the leading question, you can! Rust provides
19
- the ability to use this * method call syntax* via the ` impl ` keyword.
19
+ the ability to use this ‘ method call syntax’ via the ` impl ` keyword.
20
20
21
21
## Method calls
22
22
23
- Here' s how it works:
23
+ Here’ s how it works:
24
24
25
- ``` {rust}
26
- # #![feature(core)]
25
+ ``` rust
27
26
struct Circle {
28
27
x : f64 ,
29
28
y : f64 ,
@@ -44,15 +43,23 @@ fn main() {
44
43
45
44
This will print ` 12.566371 ` .
46
45
47
- We've made a struct that represents a circle. We then write an ` impl ` block,
48
- and inside it, define a method, ` area ` . Methods take a special first
49
- parameter, of which there are three variants: ` self ` , ` &self ` , and ` &mut self ` .
50
- You can think of this first parameter as being the ` foo ` in ` foo.bar() ` . The three
51
- variants correspond to the three kinds of things ` foo ` could be: ` self ` if it's
52
- just a value on the stack, ` &self ` if it's a reference, and ` &mut self ` if it's
53
- a mutable reference. We should default to using ` &self ` , as you should prefer
54
- borrowing over taking ownership, as well as taking immutable references
55
- over mutable ones. Here's an example of all three variants:
46
+
47
+
48
+ We’ve made a struct that represents a circle. We then write an ` impl ` block,
49
+ and inside it, define a method, ` area ` .
50
+
51
+ Methods take a special first parameter, of which there are three variants:
52
+ ` self ` , ` &self ` , and ` &mut self ` . You can think of this first parameter as
53
+ being the ` foo ` in ` foo.bar() ` . The three variants correspond to the three
54
+ kinds of things ` foo ` could be: ` self ` if it’s just a value on the stack,
55
+ ` &self ` if it’s a reference, and ` &mut self ` if it’s a mutable reference.
56
+ Because we took the ` &self ` parameter to ` area ` , we can use it just like any
57
+ other parameter. Because we know it’s a ` Circle ` , we can access the ` radius `
58
+ just like we would with any other struct.
59
+
60
+ We should default to using ` &self ` , as you should prefer borrowing over taking
61
+ ownership, as well as taking immutable references over mutable ones. Here’s an
62
+ example of all three variants:
56
63
57
64
``` rust
58
65
struct Circle {
@@ -76,20 +83,13 @@ impl Circle {
76
83
}
77
84
```
78
85
79
- Finally, as you may remember, the value of the area of a circle is ` π*r² ` .
80
- Because we took the ` &self ` parameter to ` area ` , we can use it just like any
81
- other parameter. Because we know it's a ` Circle ` , we can access the ` radius `
82
- just like we would with any other struct. An import of π and some
83
- multiplications later, and we have our area.
84
-
85
86
## Chaining method calls
86
87
87
88
So, now we know how to call a method, such as ` foo.bar() ` . But what about our
88
- original example, ` foo.bar().baz() ` ? This is called ' method chaining' , and we
89
+ original example, ` foo.bar().baz() ` ? This is called ‘ method chaining’ , and we
89
90
can do it by returning ` self ` .
90
91
91
92
```
92
- # #![feature(core)]
93
93
struct Circle {
94
94
x: f64,
95
95
y: f64,
@@ -124,13 +124,13 @@ fn grow(&self) -> Circle {
124
124
# Circle } }
125
125
```
126
126
127
- We just say we' re returning a ` Circle ` . With this method, we can grow a new
127
+ We just say we’ re returning a ` Circle ` . With this method, we can grow a new
128
128
circle to any arbitrary size.
129
129
130
130
## Static methods
131
131
132
- You can also define methods that do not take a ` self ` parameter. Here' s a
133
- pattern that' s very common in Rust code:
132
+ You can also define methods that do not take a ` self ` parameter. Here’ s a
133
+ pattern that’ s very common in Rust code:
134
134
135
135
```
136
136
struct Circle {
@@ -154,20 +154,19 @@ fn main() {
154
154
}
155
155
```
156
156
157
- This * static method* builds a new ` Circle ` for us. Note that static methods
157
+ This ‘ static method’ builds a new ` Circle ` for us. Note that static methods
158
158
are called with the ` Struct::method() ` syntax, rather than the ` ref.method() `
159
159
syntax.
160
160
161
161
## Builder Pattern
162
162
163
- Let' s say that we want our users to be able to create Circles, but we will
163
+ Let’ s say that we want our users to be able to create Circles, but we will
164
164
allow them to only set the properties they care about. Otherwise, the ` x `
165
- and ` y ` attributes will be ` 0.0 ` , and the ` radius ` will be ` 1.0 ` . Rust doesn' t
165
+ and ` y ` attributes will be ` 0.0 ` , and the ` radius ` will be ` 1.0 ` . Rust doesn’ t
166
166
have method overloading, named arguments, or variable arguments. We employ
167
167
the builder pattern instead. It looks like this:
168
168
169
169
```
170
- # #![feature(core)]
171
170
struct Circle {
172
171
x: f64,
173
172
y: f64,
@@ -224,9 +223,9 @@ fn main() {
224
223
}
225
224
```
226
225
227
- What we' ve done here is make another struct, ` CircleBuilder ` . We' ve defined our
228
- builder methods on it. We' ve also defined our ` area() ` method on ` Circle ` . We
226
+ What we’ ve done here is make another struct, ` CircleBuilder ` . We’ ve defined our
227
+ builder methods on it. We’ ve also defined our ` area() ` method on ` Circle ` . We
229
228
also made one more method on ` CircleBuilder ` : ` finalize() ` . This method creates
230
- our final ` Circle ` from the builder. Now, we' ve used the type system to enforce
229
+ our final ` Circle ` from the builder. Now, we’ ve used the type system to enforce
231
230
our concerns: we can use the methods on ` CircleBuilder ` to constrain making
232
231
` Circle ` s in any way we choose.
0 commit comments