3 ms·
Rust's iterability of Option is a good example for this because (1) it has actually bit me, but it wasn't too bad and (2) it is somewhere close to the border of
by kzrdude 3y ago
Rust's iterability of Option is a good example for this because (1) it has actually bit me, but it wasn't too bad and (2) it is somewhere close to the border of where it's reasonable/useful to treat it as a sequence or not. Logically it works, but Option is not primarily a sequence.
Then the Rust code in the article funnily enough not use traits for the `.iter()` call at all, that's just duck typing (no trait for .iter()).
- thinkharderdev 3y agoYeah. it's bit awkward to make `Option` implement `IntoIterator` and can certainly cause some weird and subtle bugs. But on the other hand it does make a whole lot of things work nicely. For example, you can turn a `Vec<Option<T>>` into a `Vet<T>` trivially with `foo.into_iter().flatten().collect()`. And generally it's nice to have Option and Vec composable.
- conaclos 3y agoI agree. Ironically the Rust standard library defines iter::once using Option::Some [O]. [0] https://doc.rust-lang.org/src/core/iter/sources/once.rs.html#55 https://doc.rust-lang.org/src/core/iter/sources/once.rs.html...