|
1 | 1 | # As input parameters
|
2 | 2 |
|
3 |
| -While Rust chooses how to capture variables on the fly mostly without type |
4 |
| -annotation, this ambiguity is not allowed when writing functions. When |
5 |
| -taking a closure as an input parameter, the closure's complete type must be |
6 |
| -annotated using one of a few `traits`. In order of decreasing restriction, |
| 3 | +While Rust chooses how to capture variables on the fly mostly without type |
| 4 | +annotation, this ambiguity is not allowed when writing functions. When |
| 5 | +taking a closure as an input parameter, the closure's complete type must be |
| 6 | +annotated using one of a few `traits`. In order of decreasing restriction, |
7 | 7 | they are:
|
8 | 8 |
|
9 | 9 | * `Fn`: the closure captures by reference (`&T`)
|
10 | 10 | * `FnMut`: the closure captures by mutable reference (`&mut T`)
|
11 | 11 | * `FnOnce`: the closure captures by value (`T`)
|
12 | 12 |
|
13 |
| -On a variable-by-variable basis, the compiler will capture variables in the |
14 |
| -least restrictive manner possible. |
| 13 | +On a variable-by-variable basis, the compiler will capture variables in the |
| 14 | +least restrictive manner possible. |
15 | 15 |
|
16 |
| -For instance, consider a parameter annotated as `FnOnce`. This specifies |
17 |
| -that the closure *may* capture by `&T`, `&mut T`, or `T`, but the compiler |
18 |
| -will ultimately choose based on how the captured variables are used in the |
| 16 | +For instance, consider a parameter annotated as `FnOnce`. This specifies |
| 17 | +that the closure *may* capture by `&T`, `&mut T`, or `T`, but the compiler |
| 18 | +will ultimately choose based on how the captured variables are used in the |
19 | 19 | closure.
|
20 | 20 |
|
21 |
| -This is because if a move is possible, then any type of borrow should also |
22 |
| -be possible. Note that the reverse is not true. If the parameter is |
23 |
| -annotated as `Fn`, then capturing variables by `&mut T` or `T` are not |
| 21 | +This is because if a move is possible, then any type of borrow should also |
| 22 | +be possible. Note that the reverse is not true. If the parameter is |
| 23 | +annotated as `Fn`, then capturing variables by `&mut T` or `T` are not |
24 | 24 | allowed.
|
25 | 25 |
|
26 |
| -In the following example, try swapping the usage of `Fn`, `FnMut`, and |
| 26 | +In the following example, try swapping the usage of `Fn`, `FnMut`, and |
27 | 27 | `FnOnce` to see what happens:
|
28 | 28 |
|
29 | 29 | ```rust,editable
|
30 | 30 | // A function which takes a closure as an argument and calls it.
|
| 31 | +// <F> denotes that F is a "Generic type parameter" |
31 | 32 | fn apply<F>(f: F) where
|
32 | 33 | // The closure takes no input and returns nothing.
|
33 | 34 | F: FnOnce() {
|
@@ -81,9 +82,11 @@ fn main() {
|
81 | 82 |
|
82 | 83 | ### See also:
|
83 | 84 |
|
84 |
| -[`std::mem::drop`][drop], [`Fn`][fn], [`FnMut`][fnmut], and [`FnOnce`][fnonce] |
| 85 | +[`std::mem::drop`][drop], [`Fn`][fn], [`FnMut`][fnmut], [Generics][generics], [where][where] and [`FnOnce`][fnonce] |
85 | 86 |
|
86 | 87 | [drop]: https://doc.rust-lang.org/std/mem/fn.drop.html
|
87 | 88 | [fn]: https://doc.rust-lang.org/std/ops/trait.Fn.html
|
88 | 89 | [fnmut]: https://doc.rust-lang.org/std/ops/trait.FnMut.html
|
89 | 90 | [fnonce]: https://doc.rust-lang.org/std/ops/trait.FnOnce.html
|
| 91 | +[generics]: ../../generics.md |
| 92 | +[where]: ../../generics/where.md |
0 commit comments