Function identity
const fn identity<T>(x: T) -> T
The 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 coercexinto a different type. -
It moves the input
xpassed 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 in a sequence of other, interesting,
functions:
use identity;
let _arr = &;
Using identity as a "do nothing" base case in a conditional:
use identity;
# let condition = true;
#
#
#
let do_stuff = if condition else ;
// Do more interesting stuff...
let _results = do_stuff;
Using identity to keep the Some variants of an iterator of Option<T>:
use identity;
let iter = .into_iter;
let filtered = iter.filter_map.;
assert_eq!;