@@ -4,15 +4,15 @@ 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
6
``` {rust,ignore}
7
- baz(bar(foo(x )));
7
+ baz(bar(foo)));
8
8
```
9
9
10
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
14
14
``` {rust,ignore}
15
- x. foo() .bar().baz();
15
+ foo.bar().baz();
16
16
```
17
17
18
18
Luckily, as you may have guessed with the leading question, you can! Rust provides
@@ -47,8 +47,8 @@ This will print `12.566371`.
47
47
We've made a struct that represents a circle. We then write an ` impl ` block,
48
48
and inside it, define a method, ` area ` . Methods take a special first
49
49
parameter, of which there are three variants: ` self ` , ` &self ` , and ` &mut self ` .
50
- You can think of this first parameter as being the ` x ` in ` x. foo()` . The three
51
- variants correspond to the three kinds of thing ` x ` could be: ` self ` if it's
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
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 you should prefer
54
54
borrowing over taking ownership, as well as taking immutable references
0 commit comments