Search⌘ K

Function Results

Explore how functions in Rust return results by defining explicit return types and using expressions without semicolons. Understand function signatures, how to produce results, and common compiler guidance to avoid errors with return statements in your Rust programs.

We'll cover the following...

So far, we’ve been pretending like all functions produce no result. But this isn’t true. In fact, it’s the opposite: every function produces a result value. By default, the result type of a function is the trusty old unit () we became so familiar with before.

How do we say what the result type of a function is? We use an “arrow”:

Rust 1.40.0
fn main() -> () {
println!("Hello, world!");
}

Adding the -> () to our main function’s definition doesn’t change anything. The default result type is unit, and now we’ve explicitly ...