Sometimes I have a need to pass an Some(t)
if b == true
and None
if b == false
. Ideally, I'd like osmething like (this doesn't compile, just for ilustratory purposes):
impl bool {
fn to_option<T>(self, v: T) -> Option<T> {
if self {
Some(v)
} else {
None
}
}
// and maybe...
fn to_option_with(self, f : F) ...
}
so I can do:
b.to_option(v)
It's just shorter than
if b { Some(v) } else { None }
Some questions:
- Does it make sense?
- What's the best way to achive it with existing stdlib? My current approach would be a custom trait, but maybe something better can be done?
- Does it make enough sense to have as a crate or even ask for inclusion in stdlib?
- What names are best?
Your opinions are appreciated.