Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch13: Clarify closure capture types #3911

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/ch13-01-closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,14 @@ compiler won’t let us use this closure with `sort_by_key`:
This is a contrived, convoluted way (that doesn’t work) to try and count the
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
the `sort_operations` vector. This closure can be called once; trying to call
it a second time wouldn’t work because `value` would no longer be in the
environment to be pushed into `sort_operations` again! Therefore, this closure
only implements `FnOnce`. When we try to compile this code, we get this error
that `value` can’t be moved out of the closure because the closure must
implement `FnMut`:
environment—into the `sort_operations` vector. The closure captures ownership
of `value` then moves `value` out of the closure by transferring ownership of
`value` to the `sort_operations` vector. This closure can be called once;
trying to call it a second time wouldn’t work because `value` would no longer
be in the environment to be pushed into `sort_operations` again! Therefore,
this closure only implements `FnOnce`. When we try to compile this code, we get
this error that `value` can’t be moved out of the closure because the closure
must implement `FnMut`:

```console
{{#include ../listings/ch13-functional-features/listing-13-08/output.txt}}
Expand Down
8 changes: 4 additions & 4 deletions src/ch13-02-iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ that takes ownership of the vector. Then we call `filter` to adapt that
iterator into a new iterator that only contains elements for which the closure
returns `true`.

The closure captures the `shoe_size` parameter from the environment and
compares the value with each shoe’s size, keeping only shoes of the size
specified. Finally, calling `collect` gathers the values returned by the
adapted iterator into a vector that’s returned by the function.
The closure captures an immutable reference to the `shoe_size` parameter from
the environment and compares the value with each shoe’s size, keeping only
shoes of the size specified. Finally, calling `collect` gathers the values
returned by the adapted iterator into a vector that’s returned by the function.

The test shows that when we call `shoes_in_size`, we get back only shoes
that have the same size as the value we specified.