How can I replace a value if it fails a predicate?
To illustrate:
assert_eq!((3-5).but_if(|v| v < 0).then(0), 0)
I thought there would be something on Option
or Result
to allow this, but I cannot find it.
How can I replace a value if it fails a predicate?
To illustrate:
assert_eq!((3-5).but_if(|v| v < 0).then(0), 0)
I thought there would be something on Option
or Result
to allow this, but I cannot find it.
But neither of these types appear here. Subtracting two numbers yields another number.
It appears you just want a traditional if-else statement:
Since you have two values that can be compared, you may also be interested in
max
:You can make your proposed syntax work, but I'm not sure it's worth it. Presented without further comment...
Update: This got a bit nicer since Rust 1.27, when
Option::filter
was added:Prior to Rust 1.27, you would have needed an iterator in order to write a single, chained expression without lots of additional custom machinery: