As per the documentation of unit type ()
, It implements the Extend
trait.
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item = ()>,
Extends a collection with the contents of an iterator.
But I don't really understand the use of it. This allowed me to do the following:
fn main() {
let mut r = ();
println!("{:?}", r); // print `()`
r.extend(vec![(), ()]);
println!("{:?}", r); // This also print `()`
}
But it does not make any sense to me.
So my question is why does the unit type implement the Extend
trait?
FromIterator
implementation, allowing you to go fromimpl Iterator<Item=()>
to()
as a bottom type – Literate.map(|_| ())
then you collect into()
, this way you consume the iterator. It's very natural code – Despoliation