Skip to content

Commit 15f57a6

Browse files
committed
Move Result::expect and Result::unwrap
1 parent 5aa8f91 commit 15f57a6

File tree

1 file changed

+76
-68
lines changed

1 file changed

+76
-68
lines changed

library/core/src/result.rs

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,82 @@ impl<T, E> Result<T, E> {
10011001
IterMut { inner: self.as_mut().ok() }
10021002
}
10031003

1004+
/////////////////////////////////////////////////////////////////////////
1005+
// Extract a value
1006+
/////////////////////////////////////////////////////////////////////////
1007+
1008+
/// Returns the contained [`Ok`] value, consuming the `self` value.
1009+
///
1010+
/// # Panics
1011+
///
1012+
/// Panics if the value is an [`Err`], with a panic message including the
1013+
/// passed message, and the content of the [`Err`].
1014+
///
1015+
///
1016+
/// # Examples
1017+
///
1018+
/// Basic usage:
1019+
///
1020+
/// ```should_panic
1021+
/// let x: Result<u32, &str> = Err("emergency failure");
1022+
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
1023+
/// ```
1024+
#[inline]
1025+
#[track_caller]
1026+
#[stable(feature = "result_expect", since = "1.4.0")]
1027+
pub fn expect(self, msg: &str) -> T
1028+
where
1029+
E: fmt::Debug,
1030+
{
1031+
match self {
1032+
Ok(t) => t,
1033+
Err(e) => unwrap_failed(msg, &e),
1034+
}
1035+
}
1036+
1037+
/// Returns the contained [`Ok`] value, consuming the `self` value.
1038+
///
1039+
/// Because this function may panic, its use is generally discouraged.
1040+
/// Instead, prefer to use pattern matching and handle the [`Err`]
1041+
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
1042+
/// [`unwrap_or_default`].
1043+
///
1044+
/// [`unwrap_or`]: Result::unwrap_or
1045+
/// [`unwrap_or_else`]: Result::unwrap_or_else
1046+
/// [`unwrap_or_default`]: Result::unwrap_or_default
1047+
///
1048+
/// # Panics
1049+
///
1050+
/// Panics if the value is an [`Err`], with a panic message provided by the
1051+
/// [`Err`]'s value.
1052+
///
1053+
///
1054+
/// # Examples
1055+
///
1056+
/// Basic usage:
1057+
///
1058+
/// ```
1059+
/// let x: Result<u32, &str> = Ok(2);
1060+
/// assert_eq!(x.unwrap(), 2);
1061+
/// ```
1062+
///
1063+
/// ```should_panic
1064+
/// let x: Result<u32, &str> = Err("emergency failure");
1065+
/// x.unwrap(); // panics with `emergency failure`
1066+
/// ```
1067+
#[inline]
1068+
#[track_caller]
1069+
#[stable(feature = "rust1", since = "1.0.0")]
1070+
pub fn unwrap(self) -> T
1071+
where
1072+
E: fmt::Debug,
1073+
{
1074+
match self {
1075+
Ok(t) => t,
1076+
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e),
1077+
}
1078+
}
1079+
10041080
////////////////////////////////////////////////////////////////////////
10051081
// Boolean operations on the values, eager and lazy
10061082
/////////////////////////////////////////////////////////////////////////
@@ -1326,74 +1402,6 @@ impl<T: Clone, E> Result<&mut T, E> {
13261402
}
13271403
}
13281404

1329-
impl<T, E: fmt::Debug> Result<T, E> {
1330-
/// Returns the contained [`Ok`] value, consuming the `self` value.
1331-
///
1332-
/// # Panics
1333-
///
1334-
/// Panics if the value is an [`Err`], with a panic message including the
1335-
/// passed message, and the content of the [`Err`].
1336-
///
1337-
///
1338-
/// # Examples
1339-
///
1340-
/// Basic usage:
1341-
///
1342-
/// ```should_panic
1343-
/// let x: Result<u32, &str> = Err("emergency failure");
1344-
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
1345-
/// ```
1346-
#[inline]
1347-
#[track_caller]
1348-
#[stable(feature = "result_expect", since = "1.4.0")]
1349-
pub fn expect(self, msg: &str) -> T {
1350-
match self {
1351-
Ok(t) => t,
1352-
Err(e) => unwrap_failed(msg, &e),
1353-
}
1354-
}
1355-
1356-
/// Returns the contained [`Ok`] value, consuming the `self` value.
1357-
///
1358-
/// Because this function may panic, its use is generally discouraged.
1359-
/// Instead, prefer to use pattern matching and handle the [`Err`]
1360-
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
1361-
/// [`unwrap_or_default`].
1362-
///
1363-
/// [`unwrap_or`]: Result::unwrap_or
1364-
/// [`unwrap_or_else`]: Result::unwrap_or_else
1365-
/// [`unwrap_or_default`]: Result::unwrap_or_default
1366-
///
1367-
/// # Panics
1368-
///
1369-
/// Panics if the value is an [`Err`], with a panic message provided by the
1370-
/// [`Err`]'s value.
1371-
///
1372-
///
1373-
/// # Examples
1374-
///
1375-
/// Basic usage:
1376-
///
1377-
/// ```
1378-
/// let x: Result<u32, &str> = Ok(2);
1379-
/// assert_eq!(x.unwrap(), 2);
1380-
/// ```
1381-
///
1382-
/// ```should_panic
1383-
/// let x: Result<u32, &str> = Err("emergency failure");
1384-
/// x.unwrap(); // panics with `emergency failure`
1385-
/// ```
1386-
#[inline]
1387-
#[track_caller]
1388-
#[stable(feature = "rust1", since = "1.0.0")]
1389-
pub fn unwrap(self) -> T {
1390-
match self {
1391-
Ok(t) => t,
1392-
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e),
1393-
}
1394-
}
1395-
}
1396-
13971405
impl<T: fmt::Debug, E> Result<T, E> {
13981406
/// Returns the contained [`Err`] value, consuming the `self` value.
13991407
///

0 commit comments

Comments
 (0)