# Try\_then method for bool

**URL:** <https://internals.rust-lang.org/t/try-then-method-for-bool/20109>\
**Category:** language design\
**Created:** [December 31, 2023, 11:59am UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109 "2023-12-31T11:59:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DemetryF](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/demetryf/32/11652_2.png) [@DemetryF](https://internals.rust-lang.org/u/DemetryF)\
**Post date:** [December 31, 2023, 11:59am UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/1 "2023-12-31T11:59:29Z")

</div>

```rust
impl bool {
  fn try_then<T, E>(self, f: impl FnOnce() -> Result<T, E>) -> Result<Option<T>, E> {
    self.then(f).transpose()
  }
}

```

That method is needed when you want to use `bool::then`, but your closure returns Result, and you must use `.transpose()?` to propagate error to the calling function

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [December 31, 2023, 8:40pm UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/2 "2023-12-31T20:40:44Z")

</div>

Can you give an example of this coming up in practice?

---

<div class="post-metadata">

**Author:** ![DemetryF](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/demetryf/32/11652_2.png) [@DemetryF](https://internals.rust-lang.org/u/DemetryF)\
**Post date:** [December 31, 2023, 9:46pm UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/3 "2023-12-31T21:46:30Z")

</div>

yes, I was writing a parser, and this piece of code looked suspiciously capable of being rewritten using standard methods,

![image](https://us1.discourse-cdn.com/flex002/uploads/rustlang/original/2X/e/e45e6251b0f3989c3379fbf25ec70e15fb38f236.png)

however, the fact that `Block::parse` returns Result prevented me from doing this very well

 ![image](https://us1.discourse-cdn.com/flex002/uploads/rustlang/original/2X/d/de4cc05db29d3e35586677ae484e11a26298c947.png)

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [January 1, 2024, 1:10am UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/4 "2024-01-01T01:10:05Z")

</div>

What's the problem with the `transpose()` call in the calling code?

---

<div class="post-metadata">

**Author:** ![DemetryF](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/demetryf/32/11652_2.png) [@DemetryF](https://internals.rust-lang.org/u/DemetryF)\
**Post date:** [January 1, 2024, 1:11am UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/5 "2024-01-01T01:11:46Z")

</div>

it adds 1 extra line of code

---

<div class="post-metadata">

**Author:** ![Noratrieb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/noratrieb/32/12440_2.png) [@Noratrieb](https://internals.rust-lang.org/u/Noratrieb)\
**Post date:** [January 1, 2024, 7:44pm UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/6 "2024-01-01T19:44:35Z")

</div>

In my opinion, the original if-else code is really nice and readable, while the transpose, but also a try\_then, would require me to think more, not really making the code nicer but only more confusing. You can also remove the outer (pink) block, which will cut down two more lines from the original if-else code.

---

<div class="post-metadata">

**Author:** ![DemetryF](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/demetryf/32/11652_2.png) [@DemetryF](https://internals.rust-lang.org/u/DemetryF)\
**Post date:** [January 1, 2024, 8:24pm UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/7 "2024-01-01T20:24:28Z")

</div>

`.transpose()` makes that second variant harder to comprehend, so `try_then`, thanks to the clear name, would make the code more understandable and short

---

<div class="post-metadata">

**Author:** ![djc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/djc/32/1592_2.png) [@djc](https://internals.rust-lang.org/u/djc)\
**Post date:** [January 2, 2024, 10:00pm UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/8 "2024-01-02T22:00:20Z")

</div>

I'd probably write that as a `match`, which is pretty nice IMO:

```rust
let else_body = match token_stream.try_consume(TokenValue::Else) {
    true => Some(Block::parse(token_stream)?),
    false => None,
};

```

This is just four lines (similar to your `.then(..).transpose()?`) option, but much easier to follow.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 1, 2024, 10:01pm UTC](https://internals.rust-lang.org/t/try-then-method-for-bool/20109/9 "2024-04-01T22:01:15Z")

</div>

Closed on April 1, 2024, 10:01pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
