core::convert::identity - Rust
Description: API documentation for the Rust `identity` fn in crate `core`.
Keywords: rust, rustlang, rust-lang, identity

# [−][src]Function core::convert::identity

    pub const fn identity<T>(x: T) -> T

🔬 This is a nightly-only experimental API. (`convert_id` #53500)

An identity function.

Two things are important to note about this function:

- It is not always equivalent to a closure like `|x| x` since the closure may coerce `x` into a different type.

- It moves the input `x` passed to the function.

While it might seem strange to have a function that just returns back the input, there are some interesting uses.

# Examples

Using `identity` to do nothing among other interesting functions:

    #![feature(convert_id)]
    use std::convert::identity;

    fn manipulation(x: u32) -> u32 {
        // Let's assume that this function does something interesting.
        x + 1
    }

    let _arr = &[identity, manipulation];Run

Using `identity` to get a function that changes nothing in a conditional:

    #![feature(convert_id)]
    use std::convert::identity;

    let do_stuff = if condition { manipulation } else { identity };

    // do more interesting stuff..

    let _results = do_stuff(42);Run

Using `identity` to keep the `Some` variants of an iterator of `Option<T>`:

    #![feature(convert_id)]
    use std::convert::identity;

    let iter = vec![Some(1), None, Some(3)].into_iter();
    let filtered = iter.filter_map(identity).collect::<Vec<_>>();
    assert_eq!(vec![1, 3], filtered);Run

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