std::ops::Fn - Rust
Description: API documentation for the Rust `Fn` trait in crate `std`.
Keywords: rust, rustlang, rust-lang, Fn

# Trait std::ops::Fn 1.0.0[−][src]

    #[lang = "fn"]
    pub trait Fn<Args>: FnMut<Args> {
        extern "rust-call" fn call(&self, args: Args) -> Self::Output;
    }

The version of the call operator that takes an immutable receiver.

Instances of `Fn` can be called repeatedly without mutating state.

This trait (`Fn`) is not to be confused with function pointers (`fn`).

`Fn` is implemented automatically by closures which only take immutable references to captured variables or don't capture anything at all, as well as (safe) function pointers (with some caveats, see their documentation for more details). Additionally, for any type `F` that implements `Fn`, `&F` implements `Fn`, too.

Since both `FnMut` and `FnOnce` are supertraits of `Fn`, any instance of `Fn` can be used as a parameter where a `FnMut` or `FnOnce` is expected.

Use `Fn` as a bound when you want to accept a parameter of function-like type and need to call it repeatedly and without mutating state (e.g. when calling it concurrently). If you do not need such strict requirements, use `FnMut` or `FnOnce` as bounds.

See the chapter on closures in The Rust Programming Language for some more information on this topic.

Also of note is the special syntax for `Fn` traits (e.g. `Fn(usize, bool) -> usize`). Those interested in the technical details of this can refer to the relevant section in the Rustonomicon.

# Examples

## Calling a closure

    let square = |x| x * x;
    assert_eq!(square(5), 25);Run

## Using a `Fn` parameter

    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);Run

## Required Methods

### `extern "rust-call" fn call(&self, args: Args) -> Self::Output`

🔬 This is a nightly-only experimental API. (`fn_traits` #29625)

Performs the call operation.

## Implementors

- `impl<'a, A, F> Fn<A> for &'a F where F: Fn<A> + ?Sized,` | [src]

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse 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`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)