|
8 | 8 | // option. This file may not be copied, modified, or distributed |
9 | 9 | // except according to those terms. |
10 | 10 |
|
11 | | -/// A version of the call operator that takes an immutable receiver. |
| 11 | +/// The version of the call operator that takes an immutable receiver. |
12 | 12 | /// |
13 | | -/// Closures only taking immutable references to captured variables |
14 | | -/// automatically implement this trait, which allows them to be invoked. |
15 | | -/// For mutably referenced captures, see [`FnMut`], and for consuming the |
16 | | -/// capture, see [`FnOnce`]. |
| 13 | +/// Instances of `Fn` can be called repeatedly without mutating state. |
17 | 14 | /// |
18 | | -/// You can use the [`Fn`] traits when you want to accept a closure as a |
19 | | -/// parameter. Since both [`FnMut`] and [`FnOnce`] are supertraits of `Fn`, any |
20 | | -/// instance of `Fn` can be used where a [`FnMut`] or [`FnOnce`] is expected. |
| 15 | +/// *This trait (`Fn`) is not to be confused with [function pointers][] |
| 16 | +/// (`fn`).* |
| 17 | +/// |
| 18 | +/// `Fn` is implemented automatically by closures which only take immutable |
| 19 | +/// references to captured variables or don't capture anything at all, as well |
| 20 | +/// as (safe) [function pointers][] (with some caveats, see their documentation |
| 21 | +/// for more details). Additionally, for any type `F` that implements `Fn`, `&F` |
| 22 | +/// implements `Fn`, too. |
| 23 | +/// |
| 24 | +/// Since both [`FnMut`] and [`FnOnce`] are supertraits of `Fn`, any |
| 25 | +/// instance of `Fn` can be used as a parameter where a [`FnMut`] or [`FnOnce`] |
| 26 | +/// is expected. |
| 27 | +/// |
| 28 | +/// Use `Fn` as a bound when you want to accept a parameter of function-like |
| 29 | +/// type and need to call it repeatedly and without mutating state (e.g. when |
| 30 | +/// calling it concurrently). If you do not need such strict requirements, use |
| 31 | +/// [`FnMut`] or [`FnOnce`] as bounds. |
21 | 32 | /// |
22 | 33 | /// See the [chapter on closures in *The Rust Programming Language*][book] for |
23 | | -/// more information about closures in general. |
| 34 | +/// some more information on this topic. |
24 | 35 | /// |
25 | 36 | /// Also of note is the special syntax for `Fn` traits (e.g. |
26 | 37 | /// `Fn(usize, bool) -> usize`). Those interested in the technical details of |
27 | | -/// this can refer to [the relevant section in *The Rustonomicon*][nomicon]. |
| 38 | +/// this can refer to [the relevant section in the *Rustonomicon*][nomicon]. |
28 | 39 | /// |
29 | 40 | /// [book]: ../../book/second-edition/ch13-01-closures.html |
30 | 41 | /// [`FnMut`]: trait.FnMut.html |
31 | 42 | /// [`FnOnce`]: trait.FnOnce.html |
| 43 | +/// [function pointers]: ../../std/primitive.fn.html |
32 | 44 | /// [nomicon]: ../../nomicon/hrtb.html |
33 | 45 | /// |
34 | 46 | /// # Examples |
@@ -61,29 +73,36 @@ pub trait Fn<Args> : FnMut<Args> { |
61 | 73 | extern "rust-call" fn call(&self, args: Args) -> Self::Output; |
62 | 74 | } |
63 | 75 |
|
64 | | -/// A version of the call operator that takes a mutable receiver. |
| 76 | +/// The version of the call operator that takes a mutable receiver. |
| 77 | +/// |
| 78 | +/// Instances of `FnMut` can be called repeatedly and may mutate state. |
65 | 79 | /// |
66 | | -/// Closures that might mutably reference captured variables automatically |
67 | | -/// implement this trait, which allows them to be invoked. For immutably |
68 | | -/// referenced captures, see [`Fn`], and for consuming the captures, see |
69 | | -/// [`FnOnce`]. |
| 80 | +/// `FnMut` is implemented automatically by closures which take mutable |
| 81 | +/// references to captured variables, as well as all types that implement |
| 82 | +/// [`Fn`], e.g. (safe) [function pointers][] (since `FnMut` is a supertrait of |
| 83 | +/// [`Fn`]). Additionally, for any type `F` that implements `FnMut`, `&mut F` |
| 84 | +/// implements `FnMut`, too. |
70 | 85 | /// |
71 | | -/// You can use the [`Fn`] traits when you want to accept a closure as a |
72 | | -/// parameter. Since [`FnOnce`] is a supertrait of `FnMut`, any instance of |
73 | | -/// `FnMut` can be used where a [`FnOnce`] is expected, and since [`Fn`] is a |
74 | | -/// subtrait of `FnMut`, any instance of [`Fn`] can be used where [`FnMut`] is |
75 | | -/// expected. |
| 86 | +/// Since [`FnOnce`] is a supertrait of `FnMut`, any instance of `FnMut` can be |
| 87 | +/// used where a [`FnOnce`] is expected, and since [`Fn`] is a subtrait of |
| 88 | +/// `FnMut`, any instance of [`Fn`] can be used where `FnMut` is expected. |
| 89 | +/// |
| 90 | +/// Use `FnMut` as a bound when you want to accept a parameter of function-like |
| 91 | +/// type and need to call it repeatedly, while allowing it to mutate state. |
| 92 | +/// If you don't want the parameter to mutate state, use [`Fn`] as a |
| 93 | +/// bound; if you don't need to call it repeatedly, use [`FnOnce`]. |
76 | 94 | /// |
77 | 95 | /// See the [chapter on closures in *The Rust Programming Language*][book] for |
78 | | -/// more information about closures in general. |
| 96 | +/// some more information on this topic. |
79 | 97 | /// |
80 | 98 | /// Also of note is the special syntax for `Fn` traits (e.g. |
81 | 99 | /// `Fn(usize, bool) -> usize`). Those interested in the technical details of |
82 | | -/// this can refer to [the relevant section in *The Rustonomicon*][nomicon]. |
| 100 | +/// this can refer to [the relevant section in the *Rustonomicon*][nomicon]. |
83 | 101 | /// |
84 | 102 | /// [book]: ../../book/second-edition/ch13-01-closures.html |
85 | | -/// [`Fn`]: trait.Fnhtml |
| 103 | +/// [`Fn`]: trait.Fn.html |
86 | 104 | /// [`FnOnce`]: trait.FnOnce.html |
| 105 | +/// [function pointers]: ../../std/primitive.fn.html |
87 | 106 | /// [nomicon]: ../../nomicon/hrtb.html |
88 | 107 | /// |
89 | 108 | /// # Examples |
@@ -127,27 +146,35 @@ pub trait FnMut<Args> : FnOnce<Args> { |
127 | 146 | extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output; |
128 | 147 | } |
129 | 148 |
|
130 | | -/// A version of the call operator that takes a by-value receiver. |
| 149 | +/// The version of the call operator that takes a by-value receiver. |
| 150 | +/// |
| 151 | +/// Instances of `FnOnce` can be called, but might not be callable multiple |
| 152 | +/// times. Because of this, if the only thing known about a type is that it |
| 153 | +/// implements `FnOnce`, it can only be called once. |
| 154 | +/// |
| 155 | +/// `FnOnce` is implemented automatically by closure that might consume captured |
| 156 | +/// variables, as well as all types that implement [`FnMut`], e.g. (safe) |
| 157 | +/// [function pointers][] (since `FnOnce` is a supertrait of [`FnMut`]). |
131 | 158 | /// |
132 | | -/// Closures that might take ownership of captured variables automatically |
133 | | -/// implement this trait, which allows them to be invoked. For immutably |
134 | | -/// referenced captures, see [`Fn`], and for mutably referenced captures, |
135 | | -/// see [`FnMut`]. |
| 159 | +/// Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any instance of |
| 160 | +/// [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected. |
136 | 161 | /// |
137 | | -/// You can use the [`Fn`] traits when you want to accept a closure as a |
138 | | -/// parameter. Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any |
139 | | -/// instance of [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected. |
| 162 | +/// Use `FnOnce` as a bound when you want to accept a parameter of function-like |
| 163 | +/// type and only need to call it once. If you need to call the parameter |
| 164 | +/// repeatedly, use [`FnMut`] as a bound; if you also need it to not mutate |
| 165 | +/// state, use [`Fn`]. |
140 | 166 | /// |
141 | 167 | /// See the [chapter on closures in *The Rust Programming Language*][book] for |
142 | | -/// more information about closures in general. |
| 168 | +/// some more information on this topic. |
143 | 169 | /// |
144 | 170 | /// Also of note is the special syntax for `Fn` traits (e.g. |
145 | 171 | /// `Fn(usize, bool) -> usize`). Those interested in the technical details of |
146 | | -/// this can refer to [the relevant section in *The Rustonomicon*][nomicon]. |
| 172 | +/// this can refer to [the relevant section in the *Rustonomicon*][nomicon]. |
147 | 173 | /// |
148 | 174 | /// [book]: ../../book/second-edition/ch13-01-closures.html |
149 | 175 | /// [`Fn`]: trait.Fn.html |
150 | 176 | /// [`FnMut`]: trait.FnMut.html |
| 177 | +/// [function pointers]: ../../std/primitive.fn.html |
151 | 178 | /// [nomicon]: ../../nomicon/hrtb.html |
152 | 179 | /// |
153 | 180 | /// # Examples |
|
0 commit comments