diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2017-12-07 23:59:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-12-07 23:59:04 +0100 |
| commit | 0b47f0226760ae059f011006342833564cfa40ae (patch) | |
| tree | 4e9965f4dbcf426da68fe6acb00a27546f320a3c | |
| parent | 655303ce9b759a2b7484a569f9a5df035a7cfdc3 (diff) | |
| parent | 5847d0babdb4bc4545d9aeb9ef89862589ae1504 (diff) | |
| download | rust-0b47f0226760ae059f011006342833564cfa40ae.tar.gz rust-0b47f0226760ae059f011006342833564cfa40ae.zip | |
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/
| -rw-r--r-- | src/libcore/option.rs | 17 | ||||
| -rw-r--r-- | src/libcore/result.rs | 10 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 12e6e843056..d8f3ec38cf3 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -338,6 +338,12 @@ 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. + /// + /// [`unwrap_or_else`]: #method.unwrap_or_else + /// /// # Examples /// /// ``` @@ -451,11 +457,16 @@ 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 /// [`None`]: #variant.None /// [`Some(v)`]: #variant.Some + /// [`ok_or_else`]: #method.ok_or_else /// /// # Examples /// @@ -644,6 +655,12 @@ 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. + /// + /// [`or_else`]: #method.or_else + /// /// # Examples /// /// ``` diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 959935242dc..2ace3d2aee8 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -625,8 +625,13 @@ 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 + /// [`or_else`]: #method.or_else /// /// # Examples /// @@ -690,8 +695,13 @@ 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 + /// [`unwrap_or_else`]: #method.unwrap_or_else /// /// # Examples /// |
