stevedonovan/gentle-intro

() is not the empty type

Opened this issue · 1 comments

In 1-basics: "The () type is the empty type, nada, void, zilch, nothing. Everything in Rust has a value, but sometimes it's just nothing." The type () actually is a type with a single value, (). It is usually referred to as the "unit type", since it is inhabited by just the one value. The empty type, the type inhabited by no values, is !.

This is important, because the unit value is useful in programs:

match v {
    Some(v) => println!("{}", v),
    None => (),
}

Yes, that was sloppy writing - it is definitely not nothing. I think I was still confused about void in those days.