Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use the word 'unwrap' to describe core 'unwrap' functions #68849

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,15 @@ impl<T> Option<T> {
}
}

/// Returns the contained value or a default.
/// Moves the value `v` out of the `Option<T>` if it is [`Some(v)`],
/// or returns a provided a default.
///
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
/// which is lazily evaluated.
///
/// [`unwrap_or_else`]: #method.unwrap_or_else
/// [`Some(v)`]: #variant.Some
///
/// # Examples
///
Expand All @@ -405,7 +407,11 @@ impl<T> Option<T> {
}
}

/// Returns the contained value or computes it from a closure.
/// Moves the value `v` out of the `Option<T>` if it is [`Some(v)`],
/// or computes it from a closure.
///
/// [`Some(v)`]: #variant.Some
/// [`None`]: #variant.None
///
/// # Examples
///
Expand All @@ -416,10 +422,10 @@ impl<T> Option<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
pub fn unwrap_or_else<F: FnOnce() -> T>(self, op: F) -> T {
match self {
Some(x) => x,
None => f(),
None => op(),
}
}

Expand Down Expand Up @@ -1023,7 +1029,7 @@ impl<T: fmt::Debug> Option<T> {
}
}

/// Unwraps an option, expecting [`None`] and returning nothing.
/// Consumes an option, expecting [`None`] and returning nothing.
///
/// # Panics
///
Expand Down
29 changes: 16 additions & 13 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,14 +792,15 @@ impl<T, E> Result<T, E> {
}
}

/// Unwraps a result, yielding the content of an [`Ok`].
/// Else, it returns `optb`.
/// Moves the value `v` out of the `Result<T, E>` if it is [`Ok(v)`],
/// or returns a provided default.
///
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
/// which is lazily evaluated.
///
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Ok(v)`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
/// [`unwrap_or_else`]: #method.unwrap_or_else
///
Expand All @@ -808,27 +809,27 @@ impl<T, E> Result<T, E> {
/// Basic usage:
///
/// ```
/// let optb = 2;
/// let default = 2;
/// let x: Result<u32, &str> = Ok(9);
/// assert_eq!(x.unwrap_or(optb), 9);
/// assert_eq!(x.unwrap_or(default), 9);
///
/// let x: Result<u32, &str> = Err("error");
/// assert_eq!(x.unwrap_or(optb), optb);
/// assert_eq!(x.unwrap_or(default), default);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn unwrap_or(self, optb: T) -> T {
pub fn unwrap_or(self, default: T) -> T {
match self {
Ok(t) => t,
Err(_) => optb,
Err(_) => default,
}
}

/// Unwraps a result, yielding the content of an [`Ok`].
/// If the value is an [`Err`] then it calls `op` with its value.
/// Moves the value `v` out of the `Result<T, E>` if it is [`Ok(v)`],
/// or computes it from a closure.
///
/// [`Ok(v)`]: enum.Result.html#variant.Ok
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
/// # Examples
///
Expand Down Expand Up @@ -931,13 +932,14 @@ impl<T: Clone, E> Result<&mut T, E> {
}

impl<T, E: fmt::Debug> Result<T, E> {
/// Unwraps a result, yielding the content of an [`Ok`].
/// Moves the value `v` out of the `Result<T, E>` if it is [`Ok(v)`].
///
/// # Panics
///
/// Panics if the value is an [`Err`], with a panic message provided by the
/// [`Err`]'s value.
///
/// [`Ok(v)`]: enum.Result.html#variant.Ok
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
Expand All @@ -964,7 +966,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
}
}

/// Unwraps a result, yielding the content of an [`Ok`].
/// Consumes a result, yielding the content of an [`Ok`].
///
/// # Panics
///
Expand Down Expand Up @@ -994,13 +996,14 @@ impl<T, E: fmt::Debug> Result<T, E> {
}

impl<T: fmt::Debug, E> Result<T, E> {
/// Unwraps a result, yielding the content of an [`Err`].
/// Moves the value `v` out of the `Result<T, E>` if it is [`Err(v)`].
///
/// # Panics
///
/// Panics if the value is an [`Ok`], with a custom panic message provided
/// by the [`Ok`]'s value.
///
/// [`Err(v)`]: enum.Result.html#variant.Err
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
Expand Down