      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194

    // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    /// A version of the call operator that takes an immutable receiver.
    ///
    /// # Examples
    ///
    /// Closures automatically implement this trait, which allows them to be
    /// invoked. Note, however, that `Fn` takes an immutable reference to any
    /// captured variables. To take a mutable capture, implement [`FnMut`], and to
    /// consume the capture, implement [`FnOnce`].
    ///
    /// [`FnMut`]: trait.FnMut.html
    /// [`FnOnce`]: trait.FnOnce.html
    ///
    /// ```
    /// let square = |x| x * x;
    /// assert_eq!(square(5), 25);
    /// ```
    ///
    /// Closures can also be passed to higher-level functions through a `Fn`
    /// parameter (or a `FnMut` or `FnOnce` parameter, which are supertraits of
    /// `Fn`).
    ///
    /// ```
    /// fn call_with_one<F>(func: F) -> usize
    ///     where F: Fn(usize) -> usize {
    ///     func(1)
    /// }
    ///
    /// let double = |x| x * 2;
    /// assert_eq!(call_with_one(double), 2);
    /// ```
    #[lang = "fn"]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_paren_sugar]
    #[fundamental] // so that regex can rely that `&str: !FnMut`
    pub trait Fn<Args> : FnMut<Args> {
        /// This is called when the call operator is used.
        #[unstable(feature = "fn_traits", issue = "29625")]
        extern "rust-call" fn call(&self, args: Args) -> Self::Output;
    }

    /// A version of the call operator that takes a mutable receiver.
    ///
    /// # Examples
    ///
    /// Closures that mutably capture variables automatically implement this trait,
    /// which allows them to be invoked.
    ///
    /// ```
    /// let mut x = 5;
    /// {
    ///     let mut square_x = || x *= x;
    ///     square_x();
    /// }
    /// assert_eq!(x, 25);
    /// ```
    ///
    /// Closures can also be passed to higher-level functions through a `FnMut`
    /// parameter (or a `FnOnce` parameter, which is a supertrait of `FnMut`).
    ///
    /// ```
    /// fn do_twice<F>(mut func: F)
    ///     where F: FnMut()
    /// {
    ///     func();
    ///     func();
    /// }
    ///
    /// let mut x: usize = 1;
    /// {
    ///     let add_two_to_x = || x += 2;
    ///     do_twice(add_two_to_x);
    /// }
    ///
    /// assert_eq!(x, 5);
    /// ```
    #[lang = "fn_mut"]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_paren_sugar]
    #[fundamental] // so that regex can rely that `&str: !FnMut`
    pub trait FnMut<Args> : FnOnce<Args> {
        /// This is called when the call operator is used.
        #[unstable(feature = "fn_traits", issue = "29625")]
        extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
    }

    /// A version of the call operator that takes a by-value receiver.
    ///
    /// # Examples
    ///
    /// By-value closures automatically implement this trait, which allows them to
    /// be invoked.
    ///
    /// ```
    /// let x = 5;
    /// let square_x = move || x * x;
    /// assert_eq!(square_x(), 25);
    /// ```
    ///
    /// By-value Closures can also be passed to higher-level functions through a
    /// `FnOnce` parameter.
    ///
    /// ```
    /// fn consume_with_relish<F>(func: F)
    ///     where F: FnOnce() -> String
    /// {
    ///     // `func` consumes its captured variables, so it cannot be run more
    ///     // than once
    ///     println!("Consumed: {}", func());
    ///
    ///     println!("Delicious!");
    ///
    ///     // Attempting to invoke `func()` again will throw a `use of moved
    ///     // value` error for `func`
    /// }
    ///
    /// let x = String::from("x");
    /// let consume_and_return_x = move || x;
    /// consume_with_relish(consume_and_return_x);
    ///
    /// // `consume_and_return_x` can no longer be invoked at this point
    /// ```
    #[lang = "fn_once"]
    #[stable(feature = "rust1", since = "1.0.0")]
    #[rustc_paren_sugar]
    #[fundamental] // so that regex can rely that `&str: !FnMut`
    pub trait FnOnce<Args> {
        /// The returned type after the call operator is used.
        #[stable(feature = "fn_once_output", since = "1.12.0")]
        type Output;

        /// This is called when the call operator is used.
        #[unstable(feature = "fn_traits", issue = "29625")]
        extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
    }

    mod impls {
        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a,A,F:?Sized> Fn<A> for &'a F
            where F : Fn<A>
        {
            extern "rust-call" fn call(&self, args: A) -> F::Output {
                (**self).call(args)
            }
        }

        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a,A,F:?Sized> FnMut<A> for &'a F
            where F : Fn<A>
        {
            extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
                (**self).call(args)
            }
        }

        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a,A,F:?Sized> FnOnce<A> for &'a F
            where F : Fn<A>
        {
            type Output = F::Output;

            extern "rust-call" fn call_once(self, args: A) -> F::Output {
                (*self).call(args)
            }
        }

        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
            where F : FnMut<A>
        {
            extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
                (*self).call_mut(args)
            }
        }

        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
            where F : FnMut<A>
        {
            type Output = F::Output;
            extern "rust-call" fn call_once(self, args: A) -> F::Output {
                (*self).call_mut(args)
            }
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)