|
|
@ -2,12 +2,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
## Description
|
|
|
|
|
|
|
|
|
|
|
|
By default, closures capture their environment by borrowing. Or you can use
|
|
|
|
By default, closures capture their environment by borrowing. Or you can use a
|
|
|
|
`move`-closure to move whole environment. However, often you want to move just
|
|
|
|
`move`-closure to move the whole environment. However, often you want to move
|
|
|
|
some variables to closure, give it copy of some data, pass it by reference, or
|
|
|
|
just some variables to the closure, give it a copy of some data, pass by
|
|
|
|
perform some other transformation.
|
|
|
|
reference, or perform some other transformation.
|
|
|
|
|
|
|
|
|
|
|
|
Use variable rebinding in separate scope for that.
|
|
|
|
Use variable rebinding in a separate scope for that.
|
|
|
|
|
|
|
|
|
|
|
|
## Example
|
|
|
|
## Example
|
|
|
|
|
|
|
|
|
|
|
@ -47,13 +47,13 @@ let closure = move || {
|
|
|
|
|
|
|
|
|
|
|
|
## Advantages
|
|
|
|
## Advantages
|
|
|
|
|
|
|
|
|
|
|
|
Copied data are grouped together with closure definition, so their purpose is
|
|
|
|
Copied data are grouped together with the closure definition, so their purpose
|
|
|
|
more clear, and they will be dropped immediately even if they are not consumed
|
|
|
|
is more clear, and they will be dropped immediately even if they are not
|
|
|
|
by closure.
|
|
|
|
consumed by the closure.
|
|
|
|
|
|
|
|
|
|
|
|
Closure uses same variable names as surrounding code whether data are copied or
|
|
|
|
The closure uses the same variable names as the surrounding code, whether data
|
|
|
|
moved.
|
|
|
|
are copied or moved.
|
|
|
|
|
|
|
|
|
|
|
|
## Disadvantages
|
|
|
|
## Disadvantages
|
|
|
|
|
|
|
|
|
|
|
|
Additional indentation of closure body.
|
|
|
|
Additional indentation of the closure body.
|
|
|
|