Functions with Return Values
Functions can return values to the code that calls them. We don't name return values, but we must declare their type after an arrow (->
). In Rust, the return value of the function is synonymous with the value of the final expression in the block of the body of a function. You can return early from a function by using the return
keyword and specifying a value, but most functions return the last expression implicitly. Here's an example of a function that returns a value:
Filename: src/main.rs
fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("The value of x is: {x}");
}
There are no function calls, macros, or even let
statements in the five
function---just the number 5
by itself. That's a perfectly valid function in Rust. Note that the function's return type is specified too, as -> i32
. Try running this code; the output should look like this:
$ cargo run
Compiling functions v0.1.0 (file:///projects/functions)
Finished dev [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/functions`
The value of x is: 5
The 5
in five
is the function's return value, which is why the return type is i32
. Let's examine this in more detail. There are two important bits: first, the line let x = five();
shows that we're using the return value of a function to initialize a variable. Because the function five
returns a 5
, that line is the same as the following:
let x = 5;
Second, the five
function has no parameters and defines the type of the return value, but the body of the function is a lonely 5
with no semicolon because it's an expression whose value we want to return.
Let's look at another example:
Filename: src/main.rs
fn main() {
let x = plus_one(5);
println!("The value of x is: {x}");
}
fn plus_one(x: i32) -> i32 {
x + 1
}
Running this code will print The value of x is: 6
. But if we place a semicolon at the end of the line containing x + 1
, changing it from an expression to a statement, we'll get an error:
Filename: src/main.rs
fn main() {
let x = plus_one(5);
println!("The value of x is: {x}");
}
fn plus_one(x: i32) -> i32 {
x + 1;
}
Compiling this code produces an error, as follows:
$ cargo run
Compiling functions v0.1.0 (file:///projects/functions)
error[E0308]: mismatched types
--> src/main.rs:7:24
|
7 | fn plus_one(x: i32) -> i32 {
| -------- ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
8 | x + 1;
| - help: remove this semicolon
The main error message, mismatched types
, reveals the core issue with this code. The definition of the function plus_one
says that it will return an i32
, but statements don't evaluate to a value, which is expressed by ()
, the unit type. Therefore, nothing is returned, which contradicts the function definition and results in an error. In this output, Rust provides a message to possibly help rectify this issue: it suggests removing the semicolon, which would fix the error.