Skip to content

Commit 43137f4

Browse files
authored
Rollup merge of rust-lang#68918 - brson:unwrapdoc, r=Dylan-DPC
Don't use the word "unwrap" to describe "unwrap" methods It's tautological, and "unwrap" is essentially Rust-specific jargon. I was teaching a newbie some Rust, and doing the usual hand-waving about error handling using unwrap. They asked what 'unwrap' means. I said look it up in the docs. The docs read (paraphrased) "unwrap unwraps". I was embarrassed. This changes all the Option/Result functions with unwrapping behavior to use a variation on a single description: > "Returns the contained `Some/Ok` value [or ...]." It also renames the closure of `Result::unwrap_or_else` to `default` for consistency with `Option`, and perhaps makes a few other small tweaks. Previous: rust-lang#68849
2 parents 699fc5f + 8251e12 commit 43137f4

File tree

2 files changed

+75
-63
lines changed

2 files changed

+75
-63
lines changed

src/libcore/option.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl<T> Option<T> {
317317
// Getting to contained values
318318
/////////////////////////////////////////////////////////////////////////
319319

320-
/// Unwraps an option, yielding the content of a [`Some`].
320+
/// Returns the contained [`Some`] value, consuming the `self` value.
321321
///
322322
/// # Panics
323323
///
@@ -348,17 +348,22 @@ impl<T> Option<T> {
348348
}
349349
}
350350

351-
/// Moves the value `v` out of the `Option<T>` if it is [`Some(v)`].
351+
/// Returns the contained [`Some`] value, consuming the `self` value.
352352
///
353-
/// In general, because this function may panic, its use is discouraged.
353+
/// Because this function may panic, its use is generally discouraged.
354354
/// Instead, prefer to use pattern matching and handle the [`None`]
355-
/// case explicitly.
355+
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
356+
/// [`unwrap_or_default`].
357+
///
358+
/// [`unwrap_or`]: #method.unwrap_or
359+
/// [`unwrap_or_else`]: #method.unwrap_or_else
360+
/// [`unwrap_or_default`]: #method.unwrap_or_default
356361
///
357362
/// # Panics
358363
///
359364
/// Panics if the self value equals [`None`].
360365
///
361-
/// [`Some(v)`]: #variant.Some
366+
/// [`Some`]: #variant.Some
362367
/// [`None`]: #variant.None
363368
///
364369
/// # Examples
@@ -382,12 +387,13 @@ impl<T> Option<T> {
382387
}
383388
}
384389

385-
/// Returns the contained value or a default.
390+
/// Returns the contained [`Some`] value or a provided default.
386391
///
387392
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
388393
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
389394
/// which is lazily evaluated.
390395
///
396+
/// [`Some`]: #variant.Some
391397
/// [`unwrap_or_else`]: #method.unwrap_or_else
392398
///
393399
/// # Examples
@@ -405,7 +411,7 @@ impl<T> Option<T> {
405411
}
406412
}
407413

408-
/// Returns the contained value or computes it from a closure.
414+
/// Returns the contained [`Some`] value or computes it from a closure.
409415
///
410416
/// # Examples
411417
///
@@ -986,7 +992,7 @@ impl<T: Clone> Option<&mut T> {
986992
}
987993

988994
impl<T: fmt::Debug> Option<T> {
989-
/// Unwraps an option, expecting [`None`] and returning nothing.
995+
/// Consumes `self` while expecting [`None`] and returning nothing.
990996
///
991997
/// # Panics
992998
///
@@ -1029,7 +1035,7 @@ impl<T: fmt::Debug> Option<T> {
10291035
}
10301036
}
10311037

1032-
/// Unwraps an option, expecting [`None`] and returning nothing.
1038+
/// Consumes `self` while expecting [`None`] and returning nothing.
10331039
///
10341040
/// # Panics
10351041
///
@@ -1074,7 +1080,7 @@ impl<T: fmt::Debug> Option<T> {
10741080
}
10751081

10761082
impl<T: Default> Option<T> {
1077-
/// Returns the contained value or a default
1083+
/// Returns the contained [`Some`] value or a default
10781084
///
10791085
/// Consumes the `self` argument then, if [`Some`], returns the contained
10801086
/// value, otherwise if [`None`], returns the [default value] for that

src/libcore/result.rs

+59-53
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,7 @@ impl<T, E> Result<T, E> {
798798
}
799799
}
800800

801-
/// Unwraps a result, yielding the content of an [`Ok`].
802-
/// Else, it returns `optb`.
801+
/// Returns the contained [`Ok`] value or a provided default.
803802
///
804803
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
805804
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
@@ -814,27 +813,25 @@ impl<T, E> Result<T, E> {
814813
/// Basic usage:
815814
///
816815
/// ```
817-
/// let optb = 2;
816+
/// let default = 2;
818817
/// let x: Result<u32, &str> = Ok(9);
819-
/// assert_eq!(x.unwrap_or(optb), 9);
818+
/// assert_eq!(x.unwrap_or(default), 9);
820819
///
821820
/// let x: Result<u32, &str> = Err("error");
822-
/// assert_eq!(x.unwrap_or(optb), optb);
821+
/// assert_eq!(x.unwrap_or(default), default);
823822
/// ```
824823
#[inline]
825824
#[stable(feature = "rust1", since = "1.0.0")]
826-
pub fn unwrap_or(self, optb: T) -> T {
825+
pub fn unwrap_or(self, default: T) -> T {
827826
match self {
828827
Ok(t) => t,
829-
Err(_) => optb,
828+
Err(_) => default,
830829
}
831830
}
832831

833-
/// Unwraps a result, yielding the content of an [`Ok`].
834-
/// If the value is an [`Err`] then it calls `op` with its value.
832+
/// Returns the contained [`Ok`] value or computes it from a closure.
835833
///
836834
/// [`Ok`]: enum.Result.html#variant.Ok
837-
/// [`Err`]: enum.Result.html#variant.Err
838835
///
839836
/// # Examples
840837
///
@@ -937,7 +934,44 @@ impl<T: Clone, E> Result<&mut T, E> {
937934
}
938935

939936
impl<T, E: fmt::Debug> Result<T, E> {
940-
/// Unwraps a result, yielding the content of an [`Ok`].
937+
/// Returns the contained [`Ok`] value, consuming the `self` value.
938+
///
939+
/// # Panics
940+
///
941+
/// Panics if the value is an [`Err`], with a panic message including the
942+
/// passed message, and the content of the [`Err`].
943+
///
944+
/// [`Ok`]: enum.Result.html#variant.Ok
945+
/// [`Err`]: enum.Result.html#variant.Err
946+
///
947+
/// # Examples
948+
///
949+
/// Basic usage:
950+
///
951+
/// ```{.should_panic}
952+
/// let x: Result<u32, &str> = Err("emergency failure");
953+
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
954+
/// ```
955+
#[inline]
956+
#[track_caller]
957+
#[stable(feature = "result_expect", since = "1.4.0")]
958+
pub fn expect(self, msg: &str) -> T {
959+
match self {
960+
Ok(t) => t,
961+
Err(e) => unwrap_failed(msg, &e),
962+
}
963+
}
964+
965+
/// Returns the contained [`Ok`] value, consuming the `self` value.
966+
///
967+
/// Because this function may panic, its use is generally discouraged.
968+
/// Instead, prefer to use pattern matching and handle the [`Err`]
969+
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
970+
/// [`unwrap_or_default`].
971+
///
972+
/// [`unwrap_or`]: #method.unwrap_or
973+
/// [`unwrap_or_else`]: #method.unwrap_or_else
974+
/// [`unwrap_or_default`]: #method.unwrap_or_default
941975
///
942976
/// # Panics
943977
///
@@ -969,13 +1003,15 @@ impl<T, E: fmt::Debug> Result<T, E> {
9691003
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e),
9701004
}
9711005
}
1006+
}
9721007

973-
/// Unwraps a result, yielding the content of an [`Ok`].
1008+
impl<T: fmt::Debug, E> Result<T, E> {
1009+
/// Returns the contained [`Err`] value, consuming the `self` value.
9741010
///
9751011
/// # Panics
9761012
///
977-
/// Panics if the value is an [`Err`], with a panic message including the
978-
/// passed message, and the content of the [`Err`].
1013+
/// Panics if the value is an [`Ok`], with a panic message including the
1014+
/// passed message, and the content of the [`Ok`].
9791015
///
9801016
/// [`Ok`]: enum.Result.html#variant.Ok
9811017
/// [`Err`]: enum.Result.html#variant.Err
@@ -985,22 +1021,20 @@ impl<T, E: fmt::Debug> Result<T, E> {
9851021
/// Basic usage:
9861022
///
9871023
/// ```{.should_panic}
988-
/// let x: Result<u32, &str> = Err("emergency failure");
989-
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
1024+
/// let x: Result<u32, &str> = Ok(10);
1025+
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
9901026
/// ```
9911027
#[inline]
9921028
#[track_caller]
993-
#[stable(feature = "result_expect", since = "1.4.0")]
994-
pub fn expect(self, msg: &str) -> T {
1029+
#[stable(feature = "result_expect_err", since = "1.17.0")]
1030+
pub fn expect_err(self, msg: &str) -> E {
9951031
match self {
996-
Ok(t) => t,
997-
Err(e) => unwrap_failed(msg, &e),
1032+
Ok(t) => unwrap_failed(msg, &t),
1033+
Err(e) => e,
9981034
}
9991035
}
1000-
}
10011036

1002-
impl<T: fmt::Debug, E> Result<T, E> {
1003-
/// Unwraps a result, yielding the content of an [`Err`].
1037+
/// Returns the contained [`Err`] value, consuming the `self` value.
10041038
///
10051039
/// # Panics
10061040
///
@@ -1031,38 +1065,10 @@ impl<T: fmt::Debug, E> Result<T, E> {
10311065
Err(e) => e,
10321066
}
10331067
}
1034-
1035-
/// Unwraps a result, yielding the content of an [`Err`].
1036-
///
1037-
/// # Panics
1038-
///
1039-
/// Panics if the value is an [`Ok`], with a panic message including the
1040-
/// passed message, and the content of the [`Ok`].
1041-
///
1042-
/// [`Ok`]: enum.Result.html#variant.Ok
1043-
/// [`Err`]: enum.Result.html#variant.Err
1044-
///
1045-
/// # Examples
1046-
///
1047-
/// Basic usage:
1048-
///
1049-
/// ```{.should_panic}
1050-
/// let x: Result<u32, &str> = Ok(10);
1051-
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
1052-
/// ```
1053-
#[inline]
1054-
#[track_caller]
1055-
#[stable(feature = "result_expect_err", since = "1.17.0")]
1056-
pub fn expect_err(self, msg: &str) -> E {
1057-
match self {
1058-
Ok(t) => unwrap_failed(msg, &t),
1059-
Err(e) => e,
1060-
}
1061-
}
10621068
}
10631069

10641070
impl<T: Default, E> Result<T, E> {
1065-
/// Returns the contained value or a default
1071+
/// Returns the contained [`Ok`] value or a default
10661072
///
10671073
/// Consumes the `self` argument then, if [`Ok`], returns the contained
10681074
/// value, otherwise if [`Err`], returns the default value for that
@@ -1101,7 +1107,7 @@ impl<T: Default, E> Result<T, E> {
11011107

11021108
#[unstable(feature = "unwrap_infallible", reason = "newly added", issue = "61695")]
11031109
impl<T, E: Into<!>> Result<T, E> {
1104-
/// Unwraps a result that can never be an [`Err`], yielding the content of the [`Ok`].
1110+
/// Returns the contained [`Ok`] value, but never panics.
11051111
///
11061112
/// Unlike [`unwrap`], this method is known to never panic on the
11071113
/// result types it is implemented for. Therefore, it can be used

0 commit comments

Comments
 (0)