@@ -4231,8 +4231,8 @@ arguments, really powerful things are possible.
4231
4231
4232
4232
Let' s make a closure:
4233
4233
4234
- ` ` ` {rust,ignore }
4235
- let add_one = | x| { 1 + x };
4234
+ ` ` ` {rust}
4235
+ let add_one = | & : x| { 1 + x };
4236
4236
4237
4237
println! (" The sum of 5 plus 1 is {}." , add_one(5));
4238
4238
` ` `
@@ -4243,9 +4243,9 @@ binding name and two parentheses, just like we would for a named function.
4243
4243
4244
4244
Let' s compare syntax. The two are pretty close:
4245
4245
4246
- ```{rust,ignore }
4247
- let add_one = |x: i32| -> i32 { 1 + x };
4248
- fn add_one (x: i32) -> i32 { 1 + x }
4246
+ ```{rust}
4247
+ let add_one = |&: x: i32| -> i32 { 1 + x };
4248
+ fn add_one (x: i32) -> i32 { 1 + x }
4249
4249
```
4250
4250
4251
4251
As you may have noticed, closures infer their argument and return types, so you
@@ -4256,11 +4256,11 @@ There's one big difference between a closure and named functions, and it's in
4256
4256
the name: a closure " closes over its environment." What does that mean? It means
4257
4257
this:
4258
4258
4259
- ` ` ` {rust,ignore }
4259
+ ` ` ` {rust}
4260
4260
fn main () {
4261
- let x = 5;
4261
+ let x: i32 = 5;
4262
4262
4263
- let printer = || { println! (" x is: {}" , x); };
4263
+ let printer = | & : | { println! (" x is: {}" , x); };
4264
4264
4265
4265
printer (); // prints " x is: 5"
4266
4266
}
@@ -4276,7 +4276,7 @@ defined. The closure borrows any variables it uses, so this will error:
4276
4276
fn main () {
4277
4277
let mut x = 5;
4278
4278
4279
- let printer = || { println! (" x is: {}" , x); };
4279
+ let printer = | & : | { println! (" x is: {}" , x); };
4280
4280
4281
4281
x = 6; // error: cannot assign to ` x` because it is borrowed
4282
4282
}
@@ -4297,30 +4297,30 @@ now. We'll talk about them more in the "Threads" section of the guide.
4297
4297
4298
4298
Closures are most useful as an argument to another function. Here' s an example:
4299
4299
4300
- ` ` ` {rust,ignore }
4301
- fn twice( x: i32, f: | i32 | - > i32 ) -> i32 {
4300
+ ` ` ` {rust}
4301
+ fn twice< F: Fn(i32) - > i 32> ( x: i32, f: F ) -> i32 {
4302
4302
f(x) + f(x)
4303
4303
}
4304
4304
4305
4305
fn main () {
4306
- let square = | x: i32| { x * x };
4306
+ let square = | & : x: i32| { x * x };
4307
4307
4308
4308
twice(5, square); // evaluates to 50
4309
4309
}
4310
4310
` ` `
4311
4311
4312
4312
Let' s break the example down, starting with `main`:
4313
4313
4314
- ```{rust,ignore }
4315
- let square = |x: i32| { x * x };
4314
+ ```{rust}
4315
+ let square = |&: x: i32| { x * x };
4316
4316
```
4317
4317
4318
4318
We' ve seen this before. We make a closure that takes an integer, and returns
4319
4319
its square.
4320
4320
4321
- ` ` ` {rust,ignore }
4322
- # fn twice( x: i32, f: |i32| -> i32 ) -> i32 { f(x) + f(x) }
4323
- # let square = |x: i32| { x * x };
4321
+ ` ` ` {rust}
4322
+ # fn twice<F: Fn(i32) -> i32>( x: i32, f: F ) -> i32 { f(x) + f(x) }
4323
+ # let square = |&: x: i32| { x * x };
4324
4324
twice(5, square); // evaluates to 50
4325
4325
` ` `
4326
4326
@@ -4342,9 +4342,9 @@ though, and that function takes an `i32` and returns an `i32`. Notice
4342
4342
how the ` | i32| -> i32` syntax looks a lot like our definition of ` square`
4343
4343
above, if we added the return type in:
4344
4344
4345
- ` ` ` {rust,ignore }
4346
- let square = | x: i32| -> i32 { x * x };
4347
- // | i32| -> i32
4345
+ ` ` ` {rust}
4346
+ let square = | & : x: i32| -> i32 { x * x };
4347
+ // | i32| -> i32
4348
4348
` ` `
4349
4349
4350
4350
This function takes an ` i32` and returns an ` i32` .
@@ -4357,8 +4357,8 @@ Finally, `twice` returns an `i32` as well.
4357
4357
4358
4358
Okay, let' s look at the body of `twice`:
4359
4359
4360
- ```{rust,ignore }
4361
- fn twice( x: i32, f: |i32| -> i32 ) -> i32 {
4360
+ ```{rust}
4361
+ fn twice<F: Fn(i32) -> i32>( x: i32, f: F ) -> i32 {
4362
4362
f(x) + f(x)
4363
4363
}
4364
4364
```
@@ -4375,8 +4375,8 @@ this technique a lot.
4375
4375
If we didn' t want to give ` square` a name, we could just define it inline.
4376
4376
This example is the same as the previous one:
4377
4377
4378
- ` ` ` {rust,ignore }
4379
- fn twice( x: i32, f: | i32 | - > i32 ) -> i32 {
4378
+ ` ` ` {rust}
4379
+ fn twice< F: Fn(i32) - > i 32> ( x: i32, f: F ) -> i32 {
4380
4380
f(x) + f(x)
4381
4381
}
4382
4382
@@ -4388,8 +4388,8 @@ fn main() {
4388
4388
A named function' s name can be used wherever you' d use a closure. Another
4389
4389
way of writing the previous example:
4390
4390
4391
- ` ` ` {rust,ignore }
4392
- fn twice( x: i32, f: | i32 | - > i32 ) -> i32 {
4391
+ ` ` ` {rust}
4392
+ fn twice< F: Fn(i32) - > i 32> ( x: i32, f: F ) -> i32 {
4393
4393
f(x) + f(x)
4394
4394
}
4395
4395
0 commit comments