///
/// # Panics
///
- /// Panics if the value is an [`Err`], with a panic message provided by the
- /// [`Err`]'s value.
+ /// Panics if the value is an [`Err`], with a panic message including the
+ /// passed message, and the content of the [`Err`].
///
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
/// Basic usage:
///
- /// ```
- /// let x: Result<u32, &str> = Ok(2);
- /// assert_eq!(x.unwrap(), 2);
- /// ```
- ///
/// ```{.should_panic}
/// let x: Result<u32, &str> = Err("emergency failure");
- /// x.unwrap(); // panics with `emergency failure`
+ /// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
/// ```
#[inline]
#[track_caller]
- #[stable(feature = "rust1", since = "1.0.0")]
- pub fn unwrap(self) -> T {
+ #[stable(feature = "result_expect", since = "1.4.0")]
+ pub fn expect(self, msg: &str) -> T {
match self {
Ok(t) => t,
- Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e),
+ Err(e) => unwrap_failed(msg, &e),
}
}
///
/// # Panics
///
- /// Panics if the value is an [`Err`], with a panic message including the
- /// passed message, and the content of the [`Err`].
+ /// Panics if the value is an [`Err`], with a panic message provided by the
+ /// [`Err`]'s value.
///
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
/// Basic usage:
///
+ /// ```
+ /// let x: Result<u32, &str> = Ok(2);
+ /// assert_eq!(x.unwrap(), 2);
+ /// ```
+ ///
/// ```{.should_panic}
/// let x: Result<u32, &str> = Err("emergency failure");
- /// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
+ /// x.unwrap(); // panics with `emergency failure`
/// ```
#[inline]
#[track_caller]
- #[stable(feature = "result_expect", since = "1.4.0")]
- pub fn expect(self, msg: &str) -> T {
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn unwrap(self) -> T {
match self {
Ok(t) => t,
- Err(e) => unwrap_failed(msg, &e),
+ Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e),
}
}
}