-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have a function to convert boolean to Option<()> in std #2227
Comments
I'd prefer having a .to_option method like this: let x = is_admin(user).to_option(calculate_important_stuff); |
@CryZe, for this case, that indeed looks like a pretty solution. though! it's not as universal as |
Why not |
Previously in this genre...
|
Could |
@jasonhansel That would be backwards incompatible. People's crates may assume that |
I used to think that a Edit: And if it did get |
I've seen a few places where this would help reduce cruft like this, where a function checks a condition, and calls a function with an optional error, using a condition to determine if the error is there:: do_thing(index, if array[index] == 0 { Some(UnexpectedZeroError) } else { None }); With this implemented, I could reduce code like this to: do_thing(index, Option::from(array[index] == 0).map(|| UnexpectedZeroError)) Even better for me, I'd love to see methods like do_thing(index, (array[index] == 0).or_some(UnexpectedZeroError); (Even nicer for my case would be |
Amusingly, this exists as
If you only have |
This is available as |
Taking inspiration from haskell (http://hackage.haskell.org/package/base-4.10.0.0/docs/Control-Monad.html#v:guard), we could have a
guard : fn(bool) -> Option<()>
function in stdThis lets us write more pleasant code. For example instead of
we can do this
basic implementation:
Or maybe even have a
Try
trait implementation forbool
? Not sure if that's beautiful thoughThe text was updated successfully, but these errors were encountered: