The unit type is Rust’s type for “nothing meaningful.” It has one value, written as (), and commonly represents a function that receives no useful input or returns no useful result. An empty function body is shorthand for returning (). section 3.1.3
A semicolon changes the return value
Rust uses the final expression of a function as its return value when that expression has no semicolon. Thus, `fn give_number() -> i32 { 8 }` returns an integer, while adding a semicolon makes the body return `()`. The compiler reports this mismatch as “expected `i32`, found `()`” and suggests removing the semicolon. Writing `fn do_something() -> () {}` makes the unit return type explicit, although `fn do_something() {}` is the shorter form. section 1.6 section 3.1.3
Use the unit type to read ordinary Rust function code:
- A function with no parameters uses empty parentheses: `fn do_something()`. section 3.1.3
- A function with no meaningful result returns `()` implicitly or explicitly. section 3.1.3
- A statement ending in `;` produces no useful value, so its value is `()`. section 1.6
- A final call returning `()` can become the enclosing function’s final result, although adding a semicolon often makes the intent clearer. section 3.1.3
fn do_something() -> () {}
Compare the explicit unit return type with the shorter function form to see that both describe a function returning nothing. section 3.1.3






