Why do functions implicitly return an empty tuple?

Hi, I wanna know why do functions implicitly return an empty tuple (). This is given as a compile error when the value the function returns mismatches the return type defined.

fn main() {
do_something(1);
}

fn do_something(x: i32) -> i32 {
let x = 5;
}

A link that points to the rust-lang/rust repo page that depicts this functionality in the compiler will be greatly appreciated.

Thanks

The empty tuple type is also known as “unit.” It's used whenever an expression or function doesn't return any data. The unit type docs have more details.

1 Like

Here's the compiler code where unit is set as the output type for a function with no explicit return type (DefaultReturn):

https://github.com/rust-lang/rust/blob/14265f9c5558e599ba8908cffc717f26389420e1/compiler/rustc_typeck/src/astconv/mod.rs#L2269

1 Like

Thank you so much.

1 Like

Thanks.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.