@@ -9,7 +9,7 @@ arguments, really powerful things are possible.
9
9
Let's make a closure:
10
10
11
11
``` {rust}
12
- let add_one = |&: x| { 1 + x };
12
+ let add_one = |x| { 1 + x };
13
13
14
14
println!("The sum of 5 plus 1 is {}.", add_one(5));
15
15
```
@@ -21,8 +21,8 @@ binding name and two parentheses, just like we would for a named function.
21
21
Let's compare syntax. The two are pretty close:
22
22
23
23
``` {rust}
24
- let add_one = |&: x: i32| -> i32 { 1 + x };
25
- fn add_one (x: i32) -> i32 { 1 + x }
24
+ let add_one = |x: i32| -> i32 { 1 + x };
25
+ fn add_one (x: i32) -> i32 { 1 + x }
26
26
```
27
27
28
28
As you may have noticed, closures infer their argument and return types, so you
37
37
fn main() {
38
38
let x: i32 = 5;
39
39
40
- let printer = |&: | { println!("x is: {}", x); };
40
+ let printer = || { println!("x is: {}", x); };
41
41
42
42
printer(); // prints "x is: 5"
43
43
}
@@ -53,7 +53,7 @@ defined. The closure borrows any variables it uses, so this will error:
53
53
fn main() {
54
54
let mut x: i32 = 5;
55
55
56
- let printer = |&: | { println!("x is: {}", x); };
56
+ let printer = || { println!("x is: {}", x); };
57
57
58
58
x = 6; // error: cannot assign to `x` because it is borrowed
59
59
}
@@ -80,7 +80,7 @@ fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 {
80
80
}
81
81
82
82
fn main() {
83
- let square = |&: x: i32| { x * x };
83
+ let square = |x: i32| { x * x };
84
84
85
85
twice(5, square); // evaluates to 50
86
86
}
@@ -89,15 +89,15 @@ fn main() {
89
89
Let's break the example down, starting with ` main ` :
90
90
91
91
``` {rust}
92
- let square = |&: x: i32| { x * x };
92
+ let square = |x: i32| { x * x };
93
93
```
94
94
95
95
We've seen this before. We make a closure that takes an integer, and returns
96
96
its square.
97
97
98
98
``` {rust}
99
99
# fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 { f(x) + f(x) }
100
- # let square = |&: x: i32| { x * x };
100
+ # let square = |x: i32| { x * x };
101
101
twice(5, square); // evaluates to 50
102
102
```
103
103
@@ -184,8 +184,8 @@ fn compose<F, G>(x: i32, f: F, g: G) -> i32
184
184
185
185
fn main() {
186
186
compose(5,
187
- |&: n: i32| { n + 42 },
188
- |&: n: i32| { n * 2 }); // evaluates to 94
187
+ |n: i32| { n + 42 },
188
+ |n: i32| { n * 2 }); // evaluates to 94
189
189
}
190
190
```
191
191
0 commit comments