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 /// A version of the call operator that takes an immutable receiver.
15 /// Closures automatically implement this trait, which allows them to be
16 /// invoked. Note, however, that `Fn` takes an immutable reference to any
17 /// captured variables. To take a mutable capture, implement [`FnMut`], and to
18 /// consume the capture, implement [`FnOnce`].
20 /// [`FnMut`]: trait.FnMut.html
21 /// [`FnOnce`]: trait.FnOnce.html
24 /// let square = |x| x * x;
25 /// assert_eq!(square(5), 25);
28 /// Closures can also be passed to higher-level functions through a `Fn`
29 /// parameter (or a `FnMut` or `FnOnce` parameter, which are supertraits of
33 /// fn call_with_one<F>(func: F) -> usize
34 /// where F: Fn(usize) -> usize {
38 /// let double = |x| x * 2;
39 /// assert_eq!(call_with_one(double), 2);
42 #[stable(feature = "rust1", since = "1.0.0")]
44 #[fundamental] // so that regex can rely that `&str: !FnMut`
45 pub trait Fn<Args> : FnMut<Args> {
46 /// This is called when the call operator is used.
47 #[unstable(feature = "fn_traits", issue = "29625")]
48 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
51 /// A version of the call operator that takes a mutable receiver.
55 /// Closures that mutably capture variables automatically implement this trait,
56 /// which allows them to be invoked.
61 /// let mut square_x = || x *= x;
64 /// assert_eq!(x, 25);
67 /// Closures can also be passed to higher-level functions through a `FnMut`
68 /// parameter (or a `FnOnce` parameter, which is a supertrait of `FnMut`).
71 /// fn do_twice<F>(mut func: F)
78 /// let mut x: usize = 1;
80 /// let add_two_to_x = || x += 2;
81 /// do_twice(add_two_to_x);
87 #[stable(feature = "rust1", since = "1.0.0")]
89 #[fundamental] // so that regex can rely that `&str: !FnMut`
90 pub trait FnMut<Args> : FnOnce<Args> {
91 /// This is called when the call operator is used.
92 #[unstable(feature = "fn_traits", issue = "29625")]
93 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
96 /// A version of the call operator that takes a by-value receiver.
100 /// By-value closures automatically implement this trait, which allows them to
105 /// let square_x = move || x * x;
106 /// assert_eq!(square_x(), 25);
109 /// By-value Closures can also be passed to higher-level functions through a
110 /// `FnOnce` parameter.
113 /// fn consume_with_relish<F>(func: F)
114 /// where F: FnOnce() -> String
116 /// // `func` consumes its captured variables, so it cannot be run more
118 /// println!("Consumed: {}", func());
120 /// println!("Delicious!");
122 /// // Attempting to invoke `func()` again will throw a `use of moved
123 /// // value` error for `func`
126 /// let x = String::from("x");
127 /// let consume_and_return_x = move || x;
128 /// consume_with_relish(consume_and_return_x);
130 /// // `consume_and_return_x` can no longer be invoked at this point
133 #[stable(feature = "rust1", since = "1.0.0")]
135 #[fundamental] // so that regex can rely that `&str: !FnMut`
136 pub trait FnOnce<Args> {
137 /// The returned type after the call operator is used.
138 #[stable(feature = "fn_once_output", since = "1.12.0")]
141 /// This is called when the call operator is used.
142 #[unstable(feature = "fn_traits", issue = "29625")]
143 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
147 #[stable(feature = "rust1", since = "1.0.0")]
148 impl<'a,A,F:?Sized> Fn<A> for &'a F
151 extern "rust-call" fn call(&self, args: A) -> F::Output {
156 #[stable(feature = "rust1", since = "1.0.0")]
157 impl<'a,A,F:?Sized> FnMut<A> for &'a F
160 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
165 #[stable(feature = "rust1", since = "1.0.0")]
166 impl<'a,A,F:?Sized> FnOnce<A> for &'a F
169 type Output = F::Output;
171 extern "rust-call" fn call_once(self, args: A) -> F::Output {
176 #[stable(feature = "rust1", since = "1.0.0")]
177 impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
180 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
181 (*self).call_mut(args)
185 #[stable(feature = "rust1", since = "1.0.0")]
186 impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
189 type Output = F::Output;
190 extern "rust-call" fn call_once(self, args: A) -> F::Output {
191 (*self).call_mut(args)