Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The empty tuple is called the unit

  • unit

  • ()

  • An empty pair of parentheses () creates an empty tuple, also called a unit.

  • Functions that don't return anything return the same unit value.

#![allow(clippy::let_unit_value)]
#![allow(clippy::unit_cmp)]

fn main() {
    let x = ();
    println!("{:?}", x);

    let y = ();
    println!("{:?}", y);

    let same = x == y;
    println!("{:?}", same);

    let res = empty();
    println!("{:?}", res);
    assert_eq!(res, ());

    let res = no_return();
    println!("{:?}", res);
    assert_eq!(res, ());
}

fn empty() {}

fn no_return() {
    println!("Hello World!");
}
()
()
true
()
Hello World!
()