Skip to content

Commit 06ea5eb

Browse files
committed
Move Result::expect_err and Result::unwrap_err
1 parent aa2aca2 commit 06ea5eb

File tree

1 file changed

+60
-56
lines changed

1 file changed

+60
-56
lines changed

library/core/src/result.rs

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,66 @@ impl<T, E> Result<T, E> {
11141114
}
11151115
}
11161116

1117+
/// Returns the contained [`Err`] value, consuming the `self` value.
1118+
///
1119+
/// # Panics
1120+
///
1121+
/// Panics if the value is an [`Ok`], with a panic message including the
1122+
/// passed message, and the content of the [`Ok`].
1123+
///
1124+
///
1125+
/// # Examples
1126+
///
1127+
/// Basic usage:
1128+
///
1129+
/// ```should_panic
1130+
/// let x: Result<u32, &str> = Ok(10);
1131+
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
1132+
/// ```
1133+
#[inline]
1134+
#[track_caller]
1135+
#[stable(feature = "result_expect_err", since = "1.17.0")]
1136+
pub fn expect_err(self, msg: &str) -> E
1137+
where
1138+
T: fmt::Debug,
1139+
{
1140+
match self {
1141+
Ok(t) => unwrap_failed(msg, &t),
1142+
Err(e) => e,
1143+
}
1144+
}
1145+
1146+
/// Returns the contained [`Err`] value, consuming the `self` value.
1147+
///
1148+
/// # Panics
1149+
///
1150+
/// Panics if the value is an [`Ok`], with a custom panic message provided
1151+
/// by the [`Ok`]'s value.
1152+
///
1153+
/// # Examples
1154+
///
1155+
/// ```should_panic
1156+
/// let x: Result<u32, &str> = Ok(2);
1157+
/// x.unwrap_err(); // panics with `2`
1158+
/// ```
1159+
///
1160+
/// ```
1161+
/// let x: Result<u32, &str> = Err("emergency failure");
1162+
/// assert_eq!(x.unwrap_err(), "emergency failure");
1163+
/// ```
1164+
#[inline]
1165+
#[track_caller]
1166+
#[stable(feature = "rust1", since = "1.0.0")]
1167+
pub fn unwrap_err(self) -> E
1168+
where
1169+
T: fmt::Debug,
1170+
{
1171+
match self {
1172+
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t),
1173+
Err(e) => e,
1174+
}
1175+
}
1176+
11171177
////////////////////////////////////////////////////////////////////////
11181178
// Boolean operations on the values, eager and lazy
11191179
/////////////////////////////////////////////////////////////////////////
@@ -1439,62 +1499,6 @@ impl<T: Clone, E> Result<&mut T, E> {
14391499
}
14401500
}
14411501

1442-
impl<T: fmt::Debug, E> Result<T, E> {
1443-
/// Returns the contained [`Err`] value, consuming the `self` value.
1444-
///
1445-
/// # Panics
1446-
///
1447-
/// Panics if the value is an [`Ok`], with a panic message including the
1448-
/// passed message, and the content of the [`Ok`].
1449-
///
1450-
///
1451-
/// # Examples
1452-
///
1453-
/// Basic usage:
1454-
///
1455-
/// ```should_panic
1456-
/// let x: Result<u32, &str> = Ok(10);
1457-
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
1458-
/// ```
1459-
#[inline]
1460-
#[track_caller]
1461-
#[stable(feature = "result_expect_err", since = "1.17.0")]
1462-
pub fn expect_err(self, msg: &str) -> E {
1463-
match self {
1464-
Ok(t) => unwrap_failed(msg, &t),
1465-
Err(e) => e,
1466-
}
1467-
}
1468-
1469-
/// Returns the contained [`Err`] value, consuming the `self` value.
1470-
///
1471-
/// # Panics
1472-
///
1473-
/// Panics if the value is an [`Ok`], with a custom panic message provided
1474-
/// by the [`Ok`]'s value.
1475-
///
1476-
/// # Examples
1477-
///
1478-
/// ```should_panic
1479-
/// let x: Result<u32, &str> = Ok(2);
1480-
/// x.unwrap_err(); // panics with `2`
1481-
/// ```
1482-
///
1483-
/// ```
1484-
/// let x: Result<u32, &str> = Err("emergency failure");
1485-
/// assert_eq!(x.unwrap_err(), "emergency failure");
1486-
/// ```
1487-
#[inline]
1488-
#[track_caller]
1489-
#[stable(feature = "rust1", since = "1.0.0")]
1490-
pub fn unwrap_err(self) -> E {
1491-
match self {
1492-
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t),
1493-
Err(e) => e,
1494-
}
1495-
}
1496-
}
1497-
14981502
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
14991503
impl<T, E: Into<!>> Result<T, E> {
15001504
/// Returns the contained [`Ok`] value, but never panics.

0 commit comments

Comments
 (0)