-
Notifications
You must be signed in to change notification settings - Fork 0
Rust Functions
The fn keyword, which allows you to declare new functions. Rust code uses snake case as the conventional style for function and variable names, in which all letters are lowercase and underscores separate words. Rust doesn’t care where you define your functions, only that they’re defined somewhere in a scope that can be seen by the caller. When a function has parameters, you can provide it with concrete values for those parameters. Technically, the concrete values are called arguments, but in casual conversation, people tend to use the words parameter and argument interchangeably for either the variables in a function’s definition or the concrete values passed in when you call a function. In function signatures, you must declare the type of each parameter. When defining multiple parameters, separate the parameter declarations with commas.
fn another_function(x: i32) {
println!("The value of x is: {x}");
}
Function bodies are made up of a series of statements optionally ending in an expression. Because Rust is an expression-based language, this is an important distinction to understand.
- Statements are instructions that perform some action and do not return a value.
- Expressions evaluate to a resultant value.
Statements do not return values. Therefore, you can’t assign a let statement to another variable, as the following code tries to do; you’ll get an error. This is different from what happens in other languages, such as C and Ruby, where the assignment returns the value of the assignment. In those languages, you can write x = y = 6 and have both x and y have the value 6; that is not the case in Rust.
fn main() {
let y = {
let x = 3;
x + 1
};
println!("The value of y is: {y}");
}
This expression:
{
let x = 3;
x + 1
}
is a block that, in this case, evaluates to 4. That value gets bound to y as part of the let statement. Note that the x + 1 line doesn’t have a semicolon at the end. Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you turn it into a statement, and it will then not return a value.
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.
fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("The value of x is: {x}");
}
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. Second, the five function has no parameters and defines the type of the return value. The body of the function is a lonely 5 with no semicolon because it’s an expression whose value we want to return.