3 ms·
Yeah both are examples of the designer being a bit "cute". The piston thing is however totally idiomatic and recommended by the piston devs, while I've never se
by Gankro 11y ago
Yeah both are examples of the designer being a bit "cute". The piston thing is however totally idiomatic and recommended by the piston devs, while I've never seen anyone actually loop over an Option in real code.
Though it wouldn't be super terrible if it was idiomatic. I personally prefer to do `s.map(|val| { ... }), but there's a contingent that argues that `map` shouldn't be used purely for side-effects, and should instead be used for, you know, mapping. We've oft argued that if you want to consume an iterator, it aught to be with a `for` loop, so it might make sense to say the same about an Option? Option is such a trivial and core type that you basically end up with 4 ways to do everything you could think of, because semantically distinct conventions all end up doing the exact same thing:
for x in data {
println!("{}", x)
}
if let Some(x) = data {
println!("{}", x)
}
data.map(|x| {
println!("{}", x)
});
match data {
Some(x) => println!("{}", x),
_ => ()
}
The fact that you can loop over an option is kind of a side-effect of a lot of our APIs taking "thing that is iterable", and evidently it was convenient to provide that for Option (which is after all just a really degenerate collection).
You can of course `match` and `if let` an Option because it's an enum, but because there's two cases and one has no state, they end up being completely equivalent (the `_ =>` branch is just an `else`).
Then finally you can `map` over an Option because... It's Option. That's what you do with optional types, dangit!