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

Rust functions return the unit by default

  • By default function return nothing, more precisely they return () the empty tuple which is called the unit.
  • We need the #[allow(clippy::let_unit_value, clippy::unit_cmp)] in the example to make Clippy, the Rust linter accept this code.
#[allow(clippy::let_unit_value, clippy::unit_cmp)]

fn main() {
    let res = hello_world();
    assert_eq!(res, ());
    println!("{:?}", res);
}

fn hello_world() {
    println!("Hello, world!");
}
Hello, world!
()