File tree Expand file tree Collapse file tree 3 files changed +4
-4
lines changed
listings/ch13-functional-features/listing-13-08 Expand file tree Collapse file tree 3 files changed +4
-4
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ $ cargo run
3
3
error[E0507]: cannot move out of `value`, a captured variable in an `FnMut` closure
4
4
--> src/main.rs:18:30
5
5
|
6
- 15 | let value = String::from("by key called");
6
+ 15 | let value = String::from("closure called");
7
7
| ----- captured outer variable
8
8
16 |
9
9
17 | list.sort_by_key(|r| {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ fn main() {
12
12
] ;
13
13
14
14
let mut sort_operations = vec ! [ ] ;
15
- let value = String :: from ( "by key called" ) ;
15
+ let value = String :: from ( "closure called" ) ;
16
16
17
17
list. sort_by_key ( |r| {
18
18
sort_operations. push ( value) ;
Original file line number Diff line number Diff line change @@ -382,7 +382,7 @@ compiler won’t let us use this closure with `sort_by_key`:
382
382
` sort_by_key ` </span >
383
383
384
384
This is a contrived, convoluted way (that doesn’t work) to try and count the
385
- number of times ` sort_by_key ` gets called when sorting ` list ` . This code
385
+ number of times ` sort_by_key ` calls the closure when sorting ` list ` . This code
386
386
attempts to do this counting by pushing ` value ` —a ` String ` from the closure’s
387
387
environment—into the ` sort_operations ` vector. The closure captures ` value `
388
388
then moves ` value ` out of the closure by transferring ownership of ` value ` to
@@ -399,7 +399,7 @@ implement `FnMut`:
399
399
400
400
The error points to the line in the closure body that moves ` value ` out of the
401
401
environment. To fix this, we need to change the closure body so that it doesn’t
402
- move values out of the environment. To count the number of times ` sort_by_key `
402
+ move values out of the environment. To count the number of times the closure
403
403
is called, keeping a counter in the environment and incrementing its value in
404
404
the closure body is a more straightforward way to calculate that. The closure
405
405
in Listing 13-9 works with ` sort_by_key ` because it is only capturing a mutable
You can’t perform that action at this time.
0 commit comments