# Impl Try for bool

**URL:** <https://internals.rust-lang.org/t/impl-try-for-bool/10534>\
**Category:** Uncategorized\
**Created:** [July 9, 2019, 6:55am UTC](https://internals.rust-lang.org/t/impl-try-for-bool/10534 "2019-07-09T06:55:54Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [July 9, 2019, 8:11am UTC](https://internals.rust-lang.org/t/impl-try-for-bool/10534/3 "2019-07-09T08:11:49Z")

</div>

Some previous places this has come up:

> [@\[Pre-RFC\] Elvis/coalesce + ternary operator](https://internals.rust-lang.org/t/pre-rfc-elvis-coalesce-ternary-operator/8537/8):
>
> Well, speaking of ?, we could spitball using the Try trait for A || B: if let Ok(v) = Try::into\_result(A) { Try::from\_ok(v), } else { B } This would be consistent with the current bool behaviour given an implementation like this: struct True; struct False; impl Try for bool { type Success = True; type Error = False; fn into\_result(self) -\> Result\<Self::Ok, Self::Error\> { if self { Ok(True) } else { Err(False) } } fn from\_error(False: Self::Error) -\> Self {…

> [@Something for coalescing; aka generalized/improved \`or\_else\`](https://internals.rust-lang.org/t/something-for-coalescing-aka-generalized-improved-or-else/9295/3):
>
> I think the only operator that would make sense is ||. It’s already lazy, as this one would be, and currently isn’t overloadable so it wouldn’t break anything. (And you can imagine a impl Try for bool where b? short-circuits on false which would be perfectly consistent with this definition, though I suspect we don’t actually want to allow ? on bool.) OTOH, maybe just the macro would be enough. A bunch of the try{} discussions have talked about “what if I just want to return the first success…

> [@Idea: Early returns and irrefutability](https://internals.rust-lang.org/t/idea-early-returns-and-irrefutability/9751/42):
>
> I don’t think you have to do any of this trickery. Parse it as let \<pat\> = (\<expr\> || \<expr\>);. Have a transformation that depending on context makes the || expand to { let temp = \<lhs\>; if temp { temp } else { \<rhs\> } or let \<pat\> = match \<lhs\> { \<pat\> =\> \<pat\>, \_ =\> \<rhs\>. Or if we’re ok with || only working with Try types rather than with refutable patterns, add impl Try for bool and desugar || to (approx) match \<lhs\>.as\_result() { Ok(t) =\> typeof::\<lhs\>::from\_ok(t), =\> \<rhs\> }. We have a …

And of course there's

> **[boolinator](https://lib.rs/crates/boolinator)**
>
> Provides the Boolinator trait, which lets you use Option and Result-style combinators with bools

I'd personally have no complaints with `bool: Try`.

---

_[View the full topic](https://internals.rust-lang.org/t/impl-try-for-bool/10534)._
