Convenience traits to help clarify/rubify a common pattern in Rust code.
use existent::When;
let x = 100u32;
assert_eq!(None, x.when(x > 200));
assert_eq!(Some(x), x.when(x < 200));
use existent::Unless;
let xs = vec!["", "Three", "", "Two", "One"];
let filtered = xs.into_iter()
.filter_map(|s| s.unless(s.is_empty()))
.collect::<Vec<&str>>();
assert_eq!(filtered, vec!["Three", "Two", "One"])
fn expensive_computation(bar: usize) -> usize {
42 * bar
}
let mut bar = 1;
assert_eq!(Some(42), (|| expensive_computation(bar)).do_unless(false));
assert_eq!(None, (|| expensive_computation(bar)).do_unless(true));
bar = 2;
assert_eq!(Some(84), (|| expensive_computation(bar)).do_when(true));
assert_eq!(None, (|| expensive_computation(bar)).do_when(false));
[dependencies.existent]
git = "https://github.com/Ryman/existent.git"