Skip to content

Commit 8251e12

Browse files
committed
Don't use the word 'unwrap' to describe core unwrapping functions
It's tautological, and Rust-specific Jargon. This changes various Option/Result methods to consistently describe unwrapping behavior using the words "return", "contain", "consume". It also renames the closure argument of `Return::unwrap_or_else` to `default` to be consistent with `Option`.
1 parent db9b578 commit 8251e12

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
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
///
@@ -980,7 +986,7 @@ impl<T: Clone> Option<&mut T> {
980986
}
981987

982988
impl<T: fmt::Debug> Option<T> {
983-
/// Unwraps an option, expecting [`None`] and returning nothing.
989+
/// Consumes `self` while expecting [`None`] and returning nothing.
984990
///
985991
/// # Panics
986992
///
@@ -1023,7 +1029,7 @@ impl<T: fmt::Debug> Option<T> {
10231029
}
10241030
}
10251031

1026-
/// Unwraps an option, expecting [`None`] and returning nothing.
1032+
/// Consumes `self` while expecting [`None`] and returning nothing.
10271033
///
10281034
/// # Panics
10291035
///
@@ -1068,7 +1074,7 @@ impl<T: fmt::Debug> Option<T> {
10681074
}
10691075

10701076
impl<T: Default> Option<T> {
1071-
/// Returns the contained value or a default
1077+
/// Returns the contained [`Some`] value or a default
10721078
///
10731079
/// Consumes the `self` argument then, if [`Some`], returns the contained
10741080
/// value, otherwise if [`None`], returns the [default value] for that

src/libcore/result.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,7 @@ impl<T, E> Result<T, E> {
792792
}
793793
}
794794

795-
/// Unwraps a result, yielding the content of an [`Ok`].
796-
/// Else, it returns `optb`.
795+
/// Returns the contained [`Ok`] value or a provided default.
797796
///
798797
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
799798
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
@@ -808,27 +807,25 @@ impl<T, E> Result<T, E> {
808807
/// Basic usage:
809808
///
810809
/// ```
811-
/// let optb = 2;
810+
/// let default = 2;
812811
/// let x: Result<u32, &str> = Ok(9);
813-
/// assert_eq!(x.unwrap_or(optb), 9);
812+
/// assert_eq!(x.unwrap_or(default), 9);
814813
///
815814
/// let x: Result<u32, &str> = Err("error");
816-
/// assert_eq!(x.unwrap_or(optb), optb);
815+
/// assert_eq!(x.unwrap_or(default), default);
817816
/// ```
818817
#[inline]
819818
#[stable(feature = "rust1", since = "1.0.0")]
820-
pub fn unwrap_or(self, optb: T) -> T {
819+
pub fn unwrap_or(self, default: T) -> T {
821820
match self {
822821
Ok(t) => t,
823-
Err(_) => optb,
822+
Err(_) => default,
824823
}
825824
}
826825

827-
/// Unwraps a result, yielding the content of an [`Ok`].
828-
/// If the value is an [`Err`] then it calls `op` with its value.
826+
/// Returns the contained [`Ok`] value or computes it from a closure.
829827
///
830828
/// [`Ok`]: enum.Result.html#variant.Ok
831-
/// [`Err`]: enum.Result.html#variant.Err
832829
///
833830
/// # Examples
834831
///
@@ -931,7 +928,7 @@ impl<T: Clone, E> Result<&mut T, E> {
931928
}
932929

933930
impl<T, E: fmt::Debug> Result<T, E> {
934-
/// Unwraps a result, yielding the content of an [`Ok`].
931+
/// Returns the contained [`Ok`] value, consuming the `self` value.
935932
///
936933
/// # Panics
937934
///
@@ -959,7 +956,16 @@ impl<T, E: fmt::Debug> Result<T, E> {
959956
}
960957
}
961958

962-
/// Unwraps a result, yielding the content of an [`Ok`].
959+
/// Returns the contained [`Ok`] value, consuming the `self` value.
960+
///
961+
/// Because this function may panic, its use is generally discouraged.
962+
/// Instead, prefer to use pattern matching and handle the [`Err`]
963+
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
964+
/// [`unwrap_or_default`].
965+
///
966+
/// [`unwrap_or`]: #method.unwrap_or
967+
/// [`unwrap_or_else`]: #method.unwrap_or_else
968+
/// [`unwrap_or_default`]: #method.unwrap_or_default
963969
///
964970
/// # Panics
965971
///
@@ -994,7 +1000,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
9941000
}
9951001

9961002
impl<T: fmt::Debug, E> Result<T, E> {
997-
/// Unwraps a result, yielding the content of an [`Err`].
1003+
/// Returns the contained [`Err`] value, consuming the `self` value.
9981004
///
9991005
/// # Panics
10001006
///
@@ -1022,7 +1028,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
10221028
}
10231029
}
10241030

1025-
/// Unwraps a result, yielding the content of an [`Err`].
1031+
/// Returns the contained [`Err`] value, consuming the `self` value.
10261032
///
10271033
/// # Panics
10281034
///
@@ -1056,7 +1062,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
10561062
}
10571063

10581064
impl<T: Default, E> Result<T, E> {
1059-
/// Returns the contained value or a default
1065+
/// Returns the contained [`Ok`] value or a default
10601066
///
10611067
/// Consumes the `self` argument then, if [`Ok`], returns the contained
10621068
/// value, otherwise if [`Err`], returns the default value for that
@@ -1095,7 +1101,7 @@ impl<T: Default, E> Result<T, E> {
10951101

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

0 commit comments

Comments
 (0)