Search code examples
rustunit-type

Why does the unit type (empty tuple) implement the `Extend` trait?


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?


Solution

  • Just a wild guess, but this piece of documentation gives a hint

    impl FromIterator<()> for ()
    

    Collapses all unit items from an iterator into one.

    This is more useful when combined with higher-level abstractions, like collecting to a Result<(), E> where you only care about errors:

    use std::io::*;
    let data = vec![1, 2, 3, 4, 5];
    let res: Result<()> = data.iter()
        .map(|x| writeln!(stdout(), "{x}"))
        .collect();
    assert!(res.is_ok());
    

    That Extend is also used in the standard library to conveniently aggregate function calls as if () were a collection.