`bool::or_else()` and `bool::or_some()`?

Ever since I've started to use bool::then() and bool::then_some(), I've run into some cases where I've wanted to use a negated bool -- in particular with is_empty(): (!val.is_empty()).then(|| vec![0]). In particular because parentheses are needed to sort out precedence, this feels a bit ugly.

Have other people run into this? Would it be worthwhile to add bool::or_else() and/or bool::or_some()?

One option is to use std::ops::Not, writing val.is_empty().not().then(|| vec![0]).

6 Likes

I think this is a case where the combinators are a problem, not a solution, and I'd personally just write the naïve

if val.is_empty() { None } else { Some(vec![0]) }

Or if you insist on combinators, then test the emptiness directly with an Option instead of going through bool:

val.first().map(|_| vec![0])
8 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.