From 351fefbb5969f241d195d3dfc1feab463ab678e1 Mon Sep 17 00:00:00 2001 From: Mazdak Date: Fri, 19 Jan 2018 01:27:59 +0100 Subject: [PATCH] add fn core::convert::id(x: T) -> T { x } --- src/libcore/convert.rs | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index e815d72d366..77a87fdf1c6 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -50,6 +50,73 @@ use fmt; +/// 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 `id` to do nothing among other interesting functions: +/// +/// ```rust +/// #![feature(convert_id)] +/// use std::convert::id; +/// +/// fn manipulation(x: u32) -> u32 { +/// // Let's assume that this function does something interesting. +/// x + 1 +/// } +/// +/// let _arr = &[id, manipulation]; +/// ``` +/// +/// Using `id` to get a function that changes nothing in a conditional: +/// +/// ```rust +/// #![feature(convert_id)] +/// use std::convert::id; +/// +/// // Let's pretend we have an interesting condition: +/// let condition = true; +/// +/// let do_stuff = if condition { manipulation } else { id }; +/// +/// // do more interesting stuff.. +/// let _results = do_stuff(42); +/// ``` +/// +/// Using `id` to concatenate an iterator of iterators: +/// +/// ```rust +/// #![feature(convert_id)] +/// use std::convert::id; +/// +/// let vec_vec = vec![vec![1, 3, 4], vec![5, 6]]; +/// let iter_iter = vec_vec.into_iter().map(Vec::into_iter); +/// let concatenated = iter_iter.flat_map(id).collect::>(); +/// assert_eq!(vec![1, 3, 4, 5, 6], concatenated); +/// ``` +/// +/// Using `id` to keep the `Some` variants of an iterator of `Option`: +/// +/// ```rust +/// #![feature(convert_id)] +/// use std::convert::id; +/// +/// let iter = vec![Some(1), None, Some(3)].into_iter(); +/// let filtered = iter.filter_map(id).collect::>(); +/// assert_eq!(vec![1, 3], filtered); +/// ``` +#[unstable(feature = "convert_id", issue = "0")] +#[inline] +pub fn id(x: T) -> T { x } + /// A type used as the error type for implementations of fallible conversion /// traits in cases where conversions cannot actually fail. /// -- 2.44.0