1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /// The version of the call operator that takes an immutable receiver.
13 /// Instances of `Fn` can be called repeatedly without mutating state.
15 /// *This trait (`Fn`) is not to be confused with [function pointers][]
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.
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`]
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.
33 /// See the [chapter on closures in *The Rust Programming Language*][book] for
34 /// some more information on this topic.
36 /// Also of note is the special syntax for `Fn` traits (e.g.
37 /// `Fn(usize, bool) -> usize`). Those interested in the technical details of
38 /// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
40 /// [book]: ../../book/second-edition/ch13-01-closures.html
41 /// [`FnMut`]: trait.FnMut.html
42 /// [`FnOnce`]: trait.FnOnce.html
43 /// [function pointers]: ../../std/primitive.fn.html
44 /// [nomicon]: ../../nomicon/hrtb.html
48 /// ## Calling a closure
51 /// let square = |x| x * x;
52 /// assert_eq!(square(5), 25);
55 /// ## Using a `Fn` parameter
58 /// fn call_with_one<F>(func: F) -> usize
59 /// where F: Fn(usize) -> usize {
63 /// let double = |x| x * 2;
64 /// assert_eq!(call_with_one(double), 2);
67 #[stable(feature = "rust1", since = "1.0.0")]
69 #[rustc_on_unimplemented(
70 on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
71 message="expected a `{Fn}<{Args}>` closure, found `{Self}`",
72 label="expected an `Fn<{Args}>` closure, found `{Self}`",
74 #[fundamental] // so that regex can rely that `&str: !FnMut`
75 pub trait Fn<Args> : FnMut<Args> {
76 /// Performs the call operation.
77 #[unstable(feature = "fn_traits", issue = "29625")]
78 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
81 /// The version of the call operator that takes a mutable receiver.
83 /// Instances of `FnMut` can be called repeatedly and may mutate state.
85 /// `FnMut` is implemented automatically by closures which take mutable
86 /// references to captured variables, as well as all types that implement
87 /// [`Fn`], e.g., (safe) [function pointers][] (since `FnMut` is a supertrait of
88 /// [`Fn`]). Additionally, for any type `F` that implements `FnMut`, `&mut F`
89 /// implements `FnMut`, too.
91 /// Since [`FnOnce`] is a supertrait of `FnMut`, any instance of `FnMut` can be
92 /// used where a [`FnOnce`] is expected, and since [`Fn`] is a subtrait of
93 /// `FnMut`, any instance of [`Fn`] can be used where `FnMut` is expected.
95 /// Use `FnMut` as a bound when you want to accept a parameter of function-like
96 /// type and need to call it repeatedly, while allowing it to mutate state.
97 /// If you don't want the parameter to mutate state, use [`Fn`] as a
98 /// bound; if you don't need to call it repeatedly, use [`FnOnce`].
100 /// See the [chapter on closures in *The Rust Programming Language*][book] for
101 /// some more information on this topic.
103 /// Also of note is the special syntax for `Fn` traits (e.g.
104 /// `Fn(usize, bool) -> usize`). Those interested in the technical details of
105 /// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
107 /// [book]: ../../book/second-edition/ch13-01-closures.html
108 /// [`Fn`]: trait.Fn.html
109 /// [`FnOnce`]: trait.FnOnce.html
110 /// [function pointers]: ../../std/primitive.fn.html
111 /// [nomicon]: ../../nomicon/hrtb.html
115 /// ## Calling a mutably capturing closure
120 /// let mut square_x = || x *= x;
123 /// assert_eq!(x, 25);
126 /// ## Using a `FnMut` parameter
129 /// fn do_twice<F>(mut func: F)
136 /// let mut x: usize = 1;
138 /// let add_two_to_x = || x += 2;
139 /// do_twice(add_two_to_x);
142 /// assert_eq!(x, 5);
145 #[stable(feature = "rust1", since = "1.0.0")]
147 #[rustc_on_unimplemented(
148 on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
149 message="expected a `{FnMut}<{Args}>` closure, found `{Self}`",
150 label="expected an `FnMut<{Args}>` closure, found `{Self}`",
152 #[fundamental] // so that regex can rely that `&str: !FnMut`
153 pub trait FnMut<Args> : FnOnce<Args> {
154 /// Performs the call operation.
155 #[unstable(feature = "fn_traits", issue = "29625")]
156 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
159 /// The version of the call operator that takes a by-value receiver.
161 /// Instances of `FnOnce` can be called, but might not be callable multiple
162 /// times. Because of this, if the only thing known about a type is that it
163 /// implements `FnOnce`, it can only be called once.
165 /// `FnOnce` is implemented automatically by closure that might consume captured
166 /// variables, as well as all types that implement [`FnMut`], e.g., (safe)
167 /// [function pointers][] (since `FnOnce` is a supertrait of [`FnMut`]).
169 /// Since both [`Fn`] and [`FnMut`] are subtraits of `FnOnce`, any instance of
170 /// [`Fn`] or [`FnMut`] can be used where a `FnOnce` is expected.
172 /// Use `FnOnce` as a bound when you want to accept a parameter of function-like
173 /// type and only need to call it once. If you need to call the parameter
174 /// repeatedly, use [`FnMut`] as a bound; if you also need it to not mutate
175 /// state, use [`Fn`].
177 /// See the [chapter on closures in *The Rust Programming Language*][book] for
178 /// some more information on this topic.
180 /// Also of note is the special syntax for `Fn` traits (e.g.
181 /// `Fn(usize, bool) -> usize`). Those interested in the technical details of
182 /// this can refer to [the relevant section in the *Rustonomicon*][nomicon].
184 /// [book]: ../../book/second-edition/ch13-01-closures.html
185 /// [`Fn`]: trait.Fn.html
186 /// [`FnMut`]: trait.FnMut.html
187 /// [function pointers]: ../../std/primitive.fn.html
188 /// [nomicon]: ../../nomicon/hrtb.html
192 /// ## Calling a by-value closure
196 /// let square_x = move || x * x;
197 /// assert_eq!(square_x(), 25);
200 /// ## Using a `FnOnce` parameter
203 /// fn consume_with_relish<F>(func: F)
204 /// where F: FnOnce() -> String
206 /// // `func` consumes its captured variables, so it cannot be run more
208 /// println!("Consumed: {}", func());
210 /// println!("Delicious!");
212 /// // Attempting to invoke `func()` again will throw a `use of moved
213 /// // value` error for `func`.
216 /// let x = String::from("x");
217 /// let consume_and_return_x = move || x;
218 /// consume_with_relish(consume_and_return_x);
220 /// // `consume_and_return_x` can no longer be invoked at this point
223 #[stable(feature = "rust1", since = "1.0.0")]
225 #[rustc_on_unimplemented(
226 on(Args="()", note="wrap the `{Self}` in a closure with no arguments: `|| {{ /* code */ }}"),
227 message="expected a `{FnOnce}<{Args}>` closure, found `{Self}`",
228 label="expected an `FnOnce<{Args}>` closure, found `{Self}`",
230 #[fundamental] // so that regex can rely that `&str: !FnMut`
231 pub trait FnOnce<Args> {
232 /// The returned type after the call operator is used.
233 #[stable(feature = "fn_once_output", since = "1.12.0")]
236 /// Performs the call operation.
237 #[unstable(feature = "fn_traits", issue = "29625")]
238 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
242 #[stable(feature = "rust1", since = "1.0.0")]
243 impl<A,F:?Sized> Fn<A> for &F
246 extern "rust-call" fn call(&self, args: A) -> F::Output {
251 #[stable(feature = "rust1", since = "1.0.0")]
252 impl<A,F:?Sized> FnMut<A> for &F
255 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
260 #[stable(feature = "rust1", since = "1.0.0")]
261 impl<A,F:?Sized> FnOnce<A> for &F
264 type Output = F::Output;
266 extern "rust-call" fn call_once(self, args: A) -> F::Output {
271 #[stable(feature = "rust1", since = "1.0.0")]
272 impl<A,F:?Sized> FnMut<A> for &mut F
275 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
276 (*self).call_mut(args)
280 #[stable(feature = "rust1", since = "1.0.0")]
281 impl<A,F:?Sized> FnOnce<A> for &mut F
284 type Output = F::Output;
285 extern "rust-call" fn call_once(self, args: A) -> F::Output {
286 (*self).call_mut(args)