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
33error[E0507]: cannot move out of `value`, a captured variable in an `FnMut` closure
44 --> src/main.rs:18:30
55 |
6- 15 | let value = String::from("by key called");
6+ 15 | let value = String::from("closure called");
77 | ----- captured outer variable
8816 |
9917 | list.sort_by_key(|r| {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ fn main() {
1212 ] ;
1313
1414 let mut sort_operations = vec ! [ ] ;
15- let value = String :: from ( "by key called" ) ;
15+ let value = String :: from ( "closure called" ) ;
1616
1717 list. sort_by_key ( |r| {
1818 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`:
382382` sort_by_key ` </span >
383383
384384This 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
386386attempts to do this counting by pushing ` value ` —a ` String ` from the closure’s
387387environment—into the ` sort_operations ` vector. The closure captures ` value `
388388then moves ` value ` out of the closure by transferring ownership of ` value ` to
@@ -399,7 +399,7 @@ implement `FnMut`:
399399
400400The error points to the line in the closure body that moves ` value ` out of the
401401environment. 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
403403is called, keeping a counter in the environment and incrementing its value in
404404the closure body is a more straightforward way to calculate that. The closure
405405in 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