Skip to content

Commit 0b47f02

Browse files
Rollup merge of #46548 - jonathanstrong:master, r=dtolnay
Recommends lazily evaluated alternatives for `Option::or` and `Result::or` Adds language to docs for `Option` and `Result` recommending the use of lazily evaluated alternatives when appropriate. These comments are intended to echo a [clippy lint] on the same topic. The [reddit discussion] may also be of interest. [clippy lint]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#or_fun_call [reddit discussion]: https://www.reddit.com/r/rust/comments/7hutqn/perils_of_optionor_and_resultor/
2 parents 655303c + 5847d0b commit 0b47f02

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/libcore/option.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ 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+
///
345+
/// [`unwrap_or_else`]: #method.unwrap_or_else
346+
///
341347
/// # Examples
342348
///
343349
/// ```
@@ -451,11 +457,16 @@ impl<T> Option<T> {
451457
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping [`Some(v)`] to
452458
/// [`Ok(v)`] and [`None`] to [`Err(err)`].
453459
///
460+
/// Arguments passed to `ok_or` are eagerly evaluated; if you are passing the
461+
/// result of a function call, it is recommended to use [`ok_or_else`], which is
462+
/// lazily evaluated.
463+
///
454464
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
455465
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
456466
/// [`Err(err)`]: ../../std/result/enum.Result.html#variant.Err
457467
/// [`None`]: #variant.None
458468
/// [`Some(v)`]: #variant.Some
469+
/// [`ok_or_else`]: #method.ok_or_else
459470
///
460471
/// # Examples
461472
///
@@ -644,6 +655,12 @@ impl<T> Option<T> {
644655

645656
/// Returns the option if it contains a value, otherwise returns `optb`.
646657
///
658+
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
659+
/// result of a function call, it is recommended to use [`or_else`], which is
660+
/// lazily evaluated.
661+
///
662+
/// [`or_else`]: #method.or_else
663+
///
647664
/// # Examples
648665
///
649666
/// ```

src/libcore/result.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,13 @@ 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
634+
/// [`or_else`]: #method.or_else
630635
///
631636
/// # Examples
632637
///
@@ -690,8 +695,13 @@ impl<T, E> Result<T, E> {
690695
/// Unwraps a result, yielding the content of an [`Ok`].
691696
/// Else, it returns `optb`.
692697
///
698+
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
699+
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
700+
/// which is lazily evaluated.
701+
///
693702
/// [`Ok`]: enum.Result.html#variant.Ok
694703
/// [`Err`]: enum.Result.html#variant.Err
704+
/// [`unwrap_or_else`]: #method.unwrap_or_else
695705
///
696706
/// # Examples
697707
///

0 commit comments

Comments
 (0)