Skip to content

Commit

Permalink
Adds language to the documentation for Option and Result suggesting
Browse files Browse the repository at this point in the history
the use of lazily evaluated alternatives when appropriate. These
comments are intended to echo a clippy lint on the same topic (see
https://rust-lang-nursery.github.io/rust-clippy/master/index.html#or_fun_call)
  • Loading branch information
Jonathan Strong committed Dec 7, 2017
1 parent 9f3b091 commit 9307917
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ impl<T> Option<T> {

/// Returns the contained value or 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.
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -451,6 +455,10 @@ impl<T> Option<T> {
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping [`Some(v)`] to
/// [`Ok(v)`] and [`None`] to [`Err(err)`].
///
/// Arguments passed to `ok_or` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use `ok_or_else`, which is
/// lazily evaluated.
///
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
/// [`Err(err)`]: ../../std/result/enum.Result.html#variant.Err
Expand Down Expand Up @@ -609,6 +617,10 @@ impl<T> Option<T> {

/// Returns the option if it contains a value, otherwise returns `optb`.
///
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use `or_else`, which is
/// lazily evaluated.
///
/// # Examples
///
/// ```
Expand Down
8 changes: 8 additions & 0 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ impl<T, E> Result<T, E> {

/// Returns `res` if the result is [`Err`], otherwise returns the [`Ok`] value of `self`.
///
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use `or_else`, which is
/// lazily evaluated.
///
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
Expand Down Expand Up @@ -690,6 +694,10 @@ impl<T, E> Result<T, E> {
/// Unwraps a result, yielding the content of an [`Ok`].
/// Else, it returns `optb`.
///
/// 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
/// [`Err`]: enum.Result.html#variant.Err
///
Expand Down

0 comments on commit 9307917

Please sign in to comment.