Skip to content

Commit 9307917

Browse files
author
Jonathan Strong
committed
Adds language to the documentation for Option and Result suggesting
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)
1 parent 9f3b091 commit 9307917

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/libcore/option.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ impl<T> Option<T> {
338338

339339
/// Returns the contained value or a default.
340340
///
341+
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
342+
/// the result of a function call, it is recommended to use `unwrap_or_else`,
343+
/// which is lazily evaluated.
344+
///
341345
/// # Examples
342346
///
343347
/// ```
@@ -451,6 +455,10 @@ impl<T> Option<T> {
451455
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping [`Some(v)`] to
452456
/// [`Ok(v)`] and [`None`] to [`Err(err)`].
453457
///
458+
/// Arguments passed to `ok_or` are eagerly evaluated; if you are passing the
459+
/// result of a function call, it is recommended to use `ok_or_else`, which is
460+
/// lazily evaluated.
461+
///
454462
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
455463
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
456464
/// [`Err(err)`]: ../../std/result/enum.Result.html#variant.Err
@@ -609,6 +617,10 @@ impl<T> Option<T> {
609617

610618
/// Returns the option if it contains a value, otherwise returns `optb`.
611619
///
620+
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
621+
/// result of a function call, it is recommended to use `or_else`, which is
622+
/// lazily evaluated.
623+
///
612624
/// # Examples
613625
///
614626
/// ```

src/libcore/result.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,10 @@ impl<T, E> Result<T, E> {
625625

626626
/// Returns `res` if the result is [`Err`], otherwise returns the [`Ok`] value of `self`.
627627
///
628+
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
629+
/// result of a function call, it is recommended to use `or_else`, which is
630+
/// lazily evaluated.
631+
///
628632
/// [`Ok`]: enum.Result.html#variant.Ok
629633
/// [`Err`]: enum.Result.html#variant.Err
630634
///
@@ -690,6 +694,10 @@ impl<T, E> Result<T, E> {
690694
/// Unwraps a result, yielding the content of an [`Ok`].
691695
/// Else, it returns `optb`.
692696
///
697+
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
698+
/// the result of a function call, it is recommended to use `unwrap_or_else`,
699+
/// which is lazily evaluated.
700+
///
693701
/// [`Ok`]: enum.Result.html#variant.Ok
694702
/// [`Err`]: enum.Result.html#variant.Err
695703
///

0 commit comments

Comments
 (0)