diff options
| author | David Tolnay <dtolnay@gmail.com> | 2021-12-30 10:27:43 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2021-12-30 10:27:43 -0800 |
| commit | 06ea5ebe4ef89cdb626930c4e8b235d430d47fee (patch) | |
| tree | 0d8338ec8c7e120060bedd5e43997ace3f6a520b | |
| parent | aa2aca2c8c9b5f07318350ecebcd535d79d0deeb (diff) | |
| download | rust-06ea5ebe4ef89cdb626930c4e8b235d430d47fee.tar.gz rust-06ea5ebe4ef89cdb626930c4e8b235d430d47fee.zip | |
Move Result::expect_err and Result::unwrap_err
| -rw-r--r-- | library/core/src/result.rs | 116 |
1 files changed, 60 insertions, 56 deletions
diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 7d0e647bd91..9cd949f7a12 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1114,6 +1114,66 @@ impl<T, E> Result<T, E> { } } + /// Returns the contained [`Err`] value, consuming the `self` value. + /// + /// # Panics + /// + /// Panics if the value is an [`Ok`], with a panic message including the + /// passed message, and the content of the [`Ok`]. + /// + /// + /// # Examples + /// + /// Basic usage: + /// + /// ```should_panic + /// let x: Result<u32, &str> = Ok(10); + /// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10` + /// ``` + #[inline] + #[track_caller] + #[stable(feature = "result_expect_err", since = "1.17.0")] + pub fn expect_err(self, msg: &str) -> E + where + T: fmt::Debug, + { + match self { + Ok(t) => unwrap_failed(msg, &t), + Err(e) => e, + } + } + + /// Returns the contained [`Err`] value, consuming the `self` value. + /// + /// # Panics + /// + /// Panics if the value is an [`Ok`], with a custom panic message provided + /// by the [`Ok`]'s value. + /// + /// # Examples + /// + /// ```should_panic + /// let x: Result<u32, &str> = Ok(2); + /// x.unwrap_err(); // panics with `2` + /// ``` + /// + /// ``` + /// let x: Result<u32, &str> = Err("emergency failure"); + /// assert_eq!(x.unwrap_err(), "emergency failure"); + /// ``` + #[inline] + #[track_caller] + #[stable(feature = "rust1", since = "1.0.0")] + pub fn unwrap_err(self) -> E + where + T: fmt::Debug, + { + match self { + Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t), + Err(e) => e, + } + } + //////////////////////////////////////////////////////////////////////// // Boolean operations on the values, eager and lazy ///////////////////////////////////////////////////////////////////////// @@ -1439,62 +1499,6 @@ impl<T: Clone, E> Result<&mut T, E> { } } -impl<T: fmt::Debug, E> Result<T, E> { - /// Returns the contained [`Err`] value, consuming the `self` value. - /// - /// # Panics - /// - /// Panics if the value is an [`Ok`], with a panic message including the - /// passed message, and the content of the [`Ok`]. - /// - /// - /// # Examples - /// - /// Basic usage: - /// - /// ```should_panic - /// let x: Result<u32, &str> = Ok(10); - /// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10` - /// ``` - #[inline] - #[track_caller] - #[stable(feature = "result_expect_err", since = "1.17.0")] - pub fn expect_err(self, msg: &str) -> E { - match self { - Ok(t) => unwrap_failed(msg, &t), - Err(e) => e, - } - } - - /// Returns the contained [`Err`] value, consuming the `self` value. - /// - /// # Panics - /// - /// Panics if the value is an [`Ok`], with a custom panic message provided - /// by the [`Ok`]'s value. - /// - /// # Examples - /// - /// ```should_panic - /// let x: Result<u32, &str> = Ok(2); - /// x.unwrap_err(); // panics with `2` - /// ``` - /// - /// ``` - /// let x: Result<u32, &str> = Err("emergency failure"); - /// assert_eq!(x.unwrap_err(), "emergency failure"); - /// ``` - #[inline] - #[track_caller] - #[stable(feature = "rust1", since = "1.0.0")] - pub fn unwrap_err(self) -> E { - match self { - Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t), - Err(e) => e, - } - } -} - #[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")] impl<T, E: Into<!>> Result<T, E> { /// Returns the contained [`Ok`] value, but never panics. |
