Functions in Rust
by Hexagon, 2 min read rust guide
Functions bundle code into reusable pieces. In Rust, they also are where the type system shows up the most, as every parameter and return value has a type that is written out. Let's look at how they work, and at the difference between statements and expressions.
Creating and Calling Functions
You've already seen one function, main. Here's another:
Functions are declared with fn, and by convention, their names are written in
snake_case. It doesn't matter if a function is defined before or after the
place where it's used.
Parameters
Parameters must always have their types written out. Rust infers types inside functions, but never in the function signature. The signature is a contract, and it should be readable on its own.
&str is a borrowed piece of text. We'll get to what "borrowed" means in the
next article.
Returning Values
The return type is written after an arrow, ->:
Notice that there's no return, and no semicolon after number * number. The
last expression in a function is automatically its return value.
Statements and Expressions
This is worth understanding properly, as it shows up everywhere in Rust.
- An expression gives back a value:
5,a + b,square(4), anif, amatchand even a block{ ... }. - A statement does something, but gives back no value.
let x = 5;is a statement.
Adding a semicolon to an expression turns it into a statement, and throws its value away. That's why this doesn't compile:
The compiler is helpful here, and suggests that you remove the semicolon.
A block is also an expression, so you can compute a value in a few steps without leaking temporary variables:
Early Returns
You can still use return, to leave a function early:
Returning 0.0 for a division by zero isn't great, as the caller can't tell it
apart from a real result. Rust has much better tools for this, and we'll see
them in the article about error handling.
The Unit Type
A function without -> returns (), called "unit". It's an empty value, used
when there's nothing to return. It's roughly what undefined is for a
JavaScript function that doesn't return anything. You'll see it in error
messages, as in "expected i32, found ()", which usually means a missing
value or an extra semicolon.
Returning Several Values
Use a tuple:
No Dynamic Types, No Type Guards
In the JavaScript guide, we spent some time on type guards, checking with
typeof that a function got the type it expected. In Rust, that's not needed.
If a function takes an i32, the compiler makes sure that it gets an i32,
and nothing else. A call like square("4") never gets to run.
When you actually want a function to accept several types, Rust has generics and traits, which are covered later in the series.
Conclusion
To really understand functions, you need to create some yourself. Let's fix the traffic light example from the last lesson:
There's still one problem: nothing stops someone from calling
handle_traffic_light("purple"). We'll fix that properly with an enum later in
the series. Up next is the most important article in the series: ownership.