From f082dde4e5bfcef53b27b5541c63f6fb6f726c90 Mon Sep 17 00:00:00 2001 From: Marcus Stollsteimer Date: Sat, 18 Nov 2023 20:10:30 +0100 Subject: [PATCH] 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. --- listings/ch13-functional-features/listing-13-08/output.txt | 2 +- listings/ch13-functional-features/listing-13-08/src/main.rs | 2 +- src/ch13-01-closures.md | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/listings/ch13-functional-features/listing-13-08/output.txt b/listings/ch13-functional-features/listing-13-08/output.txt index a910537667..8ebb87026b 100644 --- a/listings/ch13-functional-features/listing-13-08/output.txt +++ b/listings/ch13-functional-features/listing-13-08/output.txt @@ -3,7 +3,7 @@ $ cargo run error[E0507]: cannot move out of `value`, a captured variable in an `FnMut` closure --> src/main.rs:18:30 | -15 | let value = String::from("by key called"); +15 | let value = String::from("closure called"); | ----- captured outer variable 16 | 17 | list.sort_by_key(|r| { diff --git a/listings/ch13-functional-features/listing-13-08/src/main.rs b/listings/ch13-functional-features/listing-13-08/src/main.rs index 3b9c9cbdfd..1b60f1f6ad 100644 --- a/listings/ch13-functional-features/listing-13-08/src/main.rs +++ b/listings/ch13-functional-features/listing-13-08/src/main.rs @@ -12,7 +12,7 @@ fn main() { ]; let mut sort_operations = vec![]; - let value = String::from("by key called"); + let value = String::from("closure called"); list.sort_by_key(|r| { sort_operations.push(value); diff --git a/src/ch13-01-closures.md b/src/ch13-01-closures.md index f1ae324354..d9e3ac6841 100644 --- a/src/ch13-01-closures.md +++ b/src/ch13-01-closures.md @@ -382,7 +382,7 @@ compiler won’t let us use this closure with `sort_by_key`: `sort_by_key` This is a contrived, convoluted way (that doesn’t work) to try and count the -number of times `sort_by_key` gets called when sorting `list`. This code +number of times `sort_by_key` calls the closure when sorting `list`. This code attempts to do this counting by pushing `value`—a `String` from the closure’s environment—into the `sort_operations` vector. The closure captures `value` then moves `value` out of the closure by transferring ownership of `value` to @@ -399,7 +399,7 @@ implement `FnMut`: The error points to the line in the closure body that moves `value` out of the environment. To fix this, we need to change the closure body so that it doesn’t -move values out of the environment. To count the number of times `sort_by_key` +move values out of the environment. To count the number of times the closure is called, keeping a counter in the environment and incrementing its value in the closure body is a more straightforward way to calculate that. The closure in Listing 13-9 works with `sort_by_key` because it is only capturing a mutable