Skip to content

Commit

Permalink
Rollup merge of #101287 - Adam-Gleave:doc_bool_then_some, r=scottmcm
Browse files Browse the repository at this point in the history
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)!
  • Loading branch information
Dylan-DPC authored Sep 6, 2022
2 parents 36144f2 + 33afe97 commit 24f9989
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/core/src/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
/// ```
Expand Down

0 comments on commit 24f9989

Please sign in to comment.