Skip to content

Commit 6f0690b

Browse files
authored
Merge pull request #1281 from illiteratewriter/closures-generics
Add reference to Generics
2 parents ce9965b + 0913ff6 commit 6f0690b

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/fn/closures/input_parameters.md

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
# As input parameters
22

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,
77
they are:
88

99
* `Fn`: the closure captures by reference (`&T`)
1010
* `FnMut`: the closure captures by mutable reference (`&mut T`)
1111
* `FnOnce`: the closure captures by value (`T`)
1212

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.
1515

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
1919
closure.
2020

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
2424
allowed.
2525

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
2727
`FnOnce` to see what happens:
2828

2929
```rust,editable
3030
// A function which takes a closure as an argument and calls it.
31+
// <F> denotes that F is a "Generic type parameter"
3132
fn apply<F>(f: F) where
3233
// The closure takes no input and returns nothing.
3334
F: FnOnce() {
@@ -81,9 +82,11 @@ fn main() {
8182

8283
### See also:
8384

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]
8586

8687
[drop]: https://doc.rust-lang.org/std/mem/fn.drop.html
8788
[fn]: https://doc.rust-lang.org/std/ops/trait.Fn.html
8889
[fnmut]: https://doc.rust-lang.org/std/ops/trait.FnMut.html
8990
[fnonce]: https://doc.rust-lang.org/std/ops/trait.FnOnce.html
91+
[generics]: ../../generics.md
92+
[where]: ../../generics/where.md

0 commit comments

Comments
 (0)