@@ -33,8 +33,8 @@ let plus_two = |x| {
33
33
assert_eq! (4 , plus_two (2 ));
34
34
```
35
35
36
- You’ll notice a few things about closures that are a bit different than regular
37
- functions defined with ` fn ` . The first of which is that we did not need to
36
+ You’ll notice a few things about closures that are a bit different from regular
37
+ functions defined with ` fn ` . The first is that we did not need to
38
38
annotate the types of arguments the closure takes or the values it returns. We
39
39
can:
40
40
@@ -48,18 +48,18 @@ But we don’t have to. Why is this? Basically, it was chosen for ergonomic reas
48
48
While specifying the full type for named functions is helpful with things like
49
49
documentation and type inference, the types of closures are rarely documented
50
50
since they’re anonymous, and they don’t cause the kinds of error-at-a-distance
51
- that inferring named function types can.
51
+ problems that inferring named function types can.
52
52
53
53
The second is that the syntax is similar, but a bit different. I’ve added spaces
54
- here to make them look a little closer :
54
+ here for easier comparison :
55
55
56
56
``` rust
57
57
fn plus_one_v1 (x : i32 ) -> i32 { x + 1 }
58
58
let plus_one_v2 = | x : i32 | -> i32 { x + 1 };
59
59
let plus_one_v3 = | x : i32 | x + 1 ;
60
60
```
61
61
62
- Small differences, but they’re similar in ways .
62
+ Small differences, but they’re similar.
63
63
64
64
# Closures and their environment
65
65
@@ -99,7 +99,7 @@ note: previous borrow ends here
99
99
fn main() {
100
100
let mut num = 5;
101
101
let plus_num = |x| x + num;
102
-
102
+
103
103
let y = &mut num;
104
104
}
105
105
^
@@ -161,7 +161,7 @@ of `num`. So what’s the difference?
161
161
``` rust
162
162
let mut num = 5 ;
163
163
164
- {
164
+ {
165
165
let mut add_num = | x : i32 | num += x ;
166
166
167
167
add_num (5 );
@@ -180,7 +180,7 @@ If we change to a `move` closure, it’s different:
180
180
``` rust
181
181
let mut num = 5 ;
182
182
183
- {
183
+ {
184
184
let mut add_num = move | x : i32 | num += x ;
185
185
186
186
add_num (5 );
0 commit comments