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

# [−][src]Trait std::boxed::FnBox

    pub trait FnBox<A> {
        type Output;
        fn call_box(self: Box<Self>, args: A) -> Self::Output;
    }

🔬 This is a nightly-only experimental API. (`fnbox` #28796)

will be deprecated if and when `Box<FnOnce>` becomes usable

`FnBox` is a version of the `FnOnce` intended for use with boxed closure objects. The idea is that where one would normally store a `Box<dyn FnOnce()>` in a data structure, you should use `Box<dyn FnBox()>`. The two traits behave essentially the same, except that a `FnBox` closure can only be called if it is boxed. (Note that `FnBox` may be deprecated in the future if `Box<dyn FnOnce()>` closures become directly usable.)

# Examples

Here is a snippet of code which creates a hashmap full of boxed once closures and then removes them one by one, calling each closure as it is removed. Note that the type of the closures stored in the map is `Box<dyn FnBox() -> i32>` and not `Box<dyn FnOnce() -> i32>`.

    #![feature(fnbox)]

    use std::boxed::FnBox;
    use std::collections::HashMap;

    fn make_map() -> HashMap<i32, Box<dyn FnBox() -> i32>> {
        let mut map: HashMap<i32, Box<dyn FnBox() -> i32>> = HashMap::new();
        map.insert(1, Box::new(|| 22));
        map.insert(2, Box::new(|| 44));
        map
    }

    fn main() {
        let mut map = make_map();
        for i in &[1, 2] {
            let f = map.remove(&i).unwrap();
            assert_eq!(f(), i * 22);
        }
    }Run

## Associated Types

### `type Output`

🔬 This is a nightly-only experimental API. (`fnbox` #28796)

will be deprecated if and when `Box<FnOnce>` becomes usable

Loading content...

## Required methods

### `fn call_box(self: Box<Self>, args: A) -> Self::Output`

🔬 This is a nightly-only experimental API. (`fnbox` #28796)

will be deprecated if and when `Box<FnOnce>` becomes usable

Loading content...

## Implementors

### `impl<A, F> FnBox<A> for F where
F: FnOnce<A>,` [src]

#### `type Output = <F as FnOnce<A>>::Output`

🔬 This is a nightly-only experimental API. (`fnbox` #28796)

will be deprecated if and when `Box<FnOnce>` becomes usable

#### `fn call_box(self: Box<F>, args: A) -> <F as FnOnce<A>>::Output`[src]

🔬 This is a nightly-only experimental API. (`fnbox` #28796)

will be deprecated if and when `Box<FnOnce>` becomes usable

Loading content...

# 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`)