Skip to content

Commit f81b1fc

Browse files
committed
TRPL edits: method syntax
1 parent 7f43c57 commit f81b1fc

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

src/doc/trpl/method-syntax.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
Functions are great, but if you want to call a bunch of them on some data, it
44
can be awkward. Consider this code:
55

6-
```{rust,ignore}
6+
```rust,ignore
77
baz(bar(foo)));
88
```
99

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 isnt the
11+
order that the functions would get called in, thats inside-out: foo bar baz’.
12+
Wouldnt it be nice if we could do this instead?
1313

14-
```{rust,ignore}
14+
```rust,ignore
1515
foo.bar().baz();
1616
```
1717

1818
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.
2020

2121
## Method calls
2222

23-
Here's how it works:
23+
Heres how it works:
2424

25-
```{rust}
26-
# #![feature(core)]
25+
```rust
2726
struct Circle {
2827
x: f64,
2928
y: f64,
@@ -44,15 +43,23 @@ fn main() {
4443

4544
This will print `12.566371`.
4645

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:
5663

5764
```rust
5865
struct Circle {
@@ -76,20 +83,13 @@ impl Circle {
7683
}
7784
```
7885

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-
8586
## Chaining method calls
8687

8788
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
8990
can do it by returning `self`.
9091

9192
```
92-
# #![feature(core)]
9393
struct Circle {
9494
x: f64,
9595
y: f64,
@@ -124,13 +124,13 @@ fn grow(&self) -> Circle {
124124
# Circle } }
125125
```
126126

127-
We just say we're returning a `Circle`. With this method, we can grow a new
127+
We just say were returning a `Circle`. With this method, we can grow a new
128128
circle to any arbitrary size.
129129

130130
## Static methods
131131

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. Heres a
133+
pattern thats very common in Rust code:
134134

135135
```
136136
struct Circle {
@@ -154,20 +154,19 @@ fn main() {
154154
}
155155
```
156156

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
158158
are called with the `Struct::method()` syntax, rather than the `ref.method()`
159159
syntax.
160160

161161
## Builder Pattern
162162

163-
Let's say that we want our users to be able to create Circles, but we will
163+
Lets say that we want our users to be able to create Circles, but we will
164164
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 doesnt
166166
have method overloading, named arguments, or variable arguments. We employ
167167
the builder pattern instead. It looks like this:
168168

169169
```
170-
# #![feature(core)]
171170
struct Circle {
172171
x: f64,
173172
y: f64,
@@ -224,9 +223,9 @@ fn main() {
224223
}
225224
```
226225

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 weve done here is make another struct, `CircleBuilder`. Weve defined our
227+
builder methods on it. Weve also defined our `area()` method on `Circle`. We
229228
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, weve used the type system to enforce
231230
our concerns: we can use the methods on `CircleBuilder` to constrain making
232231
`Circle`s in any way we choose.

0 commit comments

Comments
 (0)