Skip to content

Commit f082dde

Browse files
committed
Small correction in ch13-01
Fix description: the example tries to count the number of times `sort_by_key` calls the closure, not how often `sort_by_key` is called. Also, use a clearer String value in the example code.
1 parent 71352de commit f082dde

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

listings/ch13-functional-features/listing-13-08/output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ $ cargo run
33
error[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
88
16 |
99
17 | list.sort_by_key(|r| {

listings/ch13-functional-features/listing-13-08/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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);

src/ch13-01-closures.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ compiler won’t let us use this closure with `sort_by_key`:
382382
`sort_by_key`</span>
383383

384384
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
386386
attempts to do this counting by pushing `value`—a `String` from the closure’s
387387
environment—into the `sort_operations` vector. The closure captures `value`
388388
then moves `value` out of the closure by transferring ownership of `value` to
@@ -399,7 +399,7 @@ implement `FnMut`:
399399

400400
The error points to the line in the closure body that moves `value` out of the
401401
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
403403
is called, keeping a counter in the environment and incrementing its value in
404404
the closure body is a more straightforward way to calculate that. The closure
405405
in Listing 13-9 works with `sort_by_key` because it is only capturing a mutable

0 commit comments

Comments
 (0)