Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a combinator for returning a Result based on a boolean condition?

For Option types, I frequently use the ok_or combinator to return errors like this:

let my_vec = [1,2,3];

let head = my_vec.first().cloned().ok_or(Error::MyError)?;

I am looking for a similar combinator or method to handle boolean values not Option types. For example:

let my_vec = [1,2,3];
let head = my_vec.is_empty().false_or(Error::MyError)?;

I often write matches to handle this, but it seems quite verbose:

let vec_is_ok = match my_vec.is_empty() {
    true => Err(...),
    false => Ok(())
}
vec_is_ok?;

Is there are more concise way to write this similar to the ok_or combinator that i can use on boolean values?

like image 837
turtle Avatar asked Dec 31 '22 15:12

turtle


2 Answers

There is a method bool::then (added in Rust 1.50.0), returning an Option<T> which you can convert into a Result<T, E>.

Your example would look like this:

use std::ops::Not;

my_vec.is_empty().not().then(|| ()).ok_or(MyError)
like image 193
Peter Hall Avatar answered Jan 04 '23 15:01

Peter Hall


I often write matches to handle this, but it seems quite verbose:

let vec_is_ok = match my_vec.is_empty() {
    true => Err(...),
    false => Ok(())
}
vec_is_ok?;

if is an expression and works with booleans:

let vec_is_ok = if my_vec.is_empty() { Err(...) } else { Ok(()) };
like image 36
Masklinn Avatar answered Jan 04 '23 17:01

Masklinn