Rust Function
# Rust Functions
Functions are ubiquitous in the Rust language.
From previous chapters, we already know the basic form of a Rust function:
fn ( )
The naming style for Rust function names is lowercase letters separated by underscores:
## Example
fn main(){
println!("Hello, world!");
another_function();
}
fn another_function(){
println!("Hello, !");
}
Output:
Hello, world!Hello, !
Note that we defined the `another_function` after the `main` function in the source code. Rust doesn't care where you define functions, as long as they are defined somewhere.
### Function Parameters
In Rust, if a function needs to have parameters, you must declare the parameter names and types:
## Example
fn main(){
another_function(5,6);
}
fn another_function(x:i32, y:i32){
println!("The value of x is: {}", x);
println!("The value of y is: {}", y);
}
Output:
The value of x is: 5 The value of y is: 6
### Statements and Expressions in Function Bodies
The body of a Rust function consists of a series of statements that can end with an expression. So far, we have only seen functions that do not end with an expression, but we have used expressions as part of statements.
A statement is a step that performs an action and does not return a value. For example:
let a = 6;
This step does not return a value, so the following statement is incorrect:
let a = (let b = 2);
An expression has a calculation step and a return value. The following are expressions (assuming the identifiers have been defined):
a = 7 b + 2 c * (a + b)
In Rust, you can write a more complex expression inside a block enclosed by {}:
## Example
fn main(){
let x =5;
let y ={
let x =3;
x +1
};
println!("The value of x is: {}", x);
println!("The value of y is: {}", y);
}
Output:
The value of x is: 5 The value of y is: 4
Clearly, this program contains an expression block:
{ let x = 3; x + 1};
And you can use function statements inside the block. The last step is an expression, and the result of this expression is the value represented by the entire expression block. This kind of expression block is called a function body expression.
Note: There is no semicolon after `x + 1`, otherwise it would become a statement!
This expression block is a valid function body. Moreover, in Rust, function definitions can be nested:
## Example
fn main(){
fn five()->i32{
5
}
println!("The value of five() is: {}", five());
}
### Function Return Values
In the previous nested example, we already saw how Rust functions declare return value types: use `->` after the parameter declaration to declare the function's return value type (not `:`).
In the function body, you can end the function execution and return a value of the appropriate type at any time using the `return` keyword. This is also the approach closest to most developers' experience:
## Example
fn add(a:i32, b:i32)->i32{
return a + b;
}
However, Rust does not support automatic return value type inference! If the function's return value type is not explicitly declared, the function will be considered a "pure procedure" and is not allowed to produce a return value. There cannot be a return value expression after `return`. The purpose of this is to make public functions form a visible interface.
**Note:** A function body expression is not equivalent to a function body; it cannot use the **return** keyword.
**
YouTip