Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

unit-type

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?

like image 453
Abdul Niyas P M Avatar asked Feb 12 '26 13:02

Abdul Niyas P M


1 Answers

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.

like image 65
Alexey Larionov Avatar answered Feb 17 '26 13:02

Alexey Larionov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!