diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 93ee11aac36b0..5b8d416035555 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -1002,6 +1002,34 @@ impl Result, E> { } } +impl Result { + /// Extract the value of a [`Result`] where the [`Err`] and [`Ok`] types are the same. + /// + /// This function does not panic in any way, the [`Result`] type is always either an [`Ok`] + /// or an [`Err`]. If the types of the variants are the same, it is possible to extract a value + /// either from one or the other variant. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_into_inner)] + /// + /// let x: Result = Ok(16); + /// assert_eq!(x.into_inner(), 16); + /// + /// let x: Result = Err(42); + /// assert_eq!(x.into_inner(), 42); + /// ``` + #[unstable(feature = "result_into_inner", issue = "0")] + #[inline] + pub fn into_inner(self) -> T { + match self { + Ok(value) => value, + Err(value) => value, + } + } +} + // This is a separate function to reduce the code size of the methods #[inline(never)] #[cold]