diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-09-06 16:34:43 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-06 16:34:43 +0530 |
| commit | 24f998932e1179c16354ad92f2da6204ebcac86e (patch) | |
| tree | ae0e2927c3f6f1dbd749c7c424a9e8749269fdba | |
| parent | 36144f2b3d229a510af02914686a12ae7708265d (diff) | |
| parent | 33afe9724aff279cd6a9fc1cbbdca5aa8a721e30 (diff) | |
| download | rust-24f998932e1179c16354ad92f2da6204ebcac86e.tar.gz rust-24f998932e1179c16354ad92f2da6204ebcac86e.zip | |
Rollup merge of #101287 - Adam-Gleave:doc_bool_then_some, r=scottmcm
Document eager evaluation of `bool::then_some` argument
I encountered this earlier today and thought maybe `bool::then_some` could use a little addition to the documentation.
It's pretty obvious with familiarity and from looking at the implementation, but the argument for `then_some` is eagerly evaluated, which means if you do the following (as I did), you can have a problem:
```rust
// Oops!
let _ = something
.has_another_thing()
.then_some(something.another_thing_or_panic());
```
A note, similar to other methods with eagerly-evaluated arguments and a lazy alternative (`Option::or`, for example), could help point this out to people who forget (like me)!
| -rw-r--r-- | library/core/src/bool.rs | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index f7a8aa0d921..7667a650837 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -6,6 +6,12 @@ impl bool { /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html), /// or `None` otherwise. /// + /// Arguments passed to `then_some` are eagerly evaluated; if you are + /// passing the result of a function call, it is recommended to use + /// [`then`], which is lazily evaluated. + /// + /// [`then`]: bool::then + /// /// # Examples /// /// ``` |
