Rust Functions
Functions are ubiquitous in the Rust language.
Through the previous chapters, we have already learned the basic form of Rust functions:
fn <函数名> ( <参数> ) <函数体>
The naming style for Rust function names is lowercase letters separated by underscores:
Example
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Hello, example!");
}
Output:
Hello, world! Hello, example!
Note that we defined another_function after the main function in the source code. Rust doesn't care where you define functions, you just need to define them somewhere.
Function Parameters
In Rust, if a function needs parameters, you must declare the parameter names and types:
Example
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:
x 的值为 : 5 y 的值为 : 6
Statements and Expressions in Function Bodies
Rust function bodies consist of a series of statements (Statements) that can end with an expression (Expression). So far, we have only seen functions that do not end with an expression, but we have already used expressions as part of statements.
A statement is a step that performs some action and has no return value. For example:
let a = 6;
This step has no return value, so the following statement is incorrect:
let a = (let b = 2);
An expression has computation steps and a return value. The following are expressions (assuming the identifiers that appear have already been defined):
a = 7 b + 2 c * (a + b)
In Rust, you can use a{}block enclosed by {} to write a more complex expression:
Example
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:
x 的值为 : 5 y 的值为 : 4
Obviously, this program contains an expression block:
{
let x = 3;
x + 1
};
Furthermore, function statements can be used in the block, and the last step is an expression. The result value of this expression is the value represented by the entire expression block. This kind of expression block is called a function body expression.
Note:x + 1There is no semicolon after it, otherwise it will become a statement!
This kind of expression block is a valid function body. Also, in Rust, function definitions can be nested:
Example
fn five() -> i32 {
5
}
println!("The value of five() is: {}", five());
}
Function Return Values
The previous nested example has already shown the way Rust functions declare return value types: after the parameter declarations, use->to declare the function's return value type (not: )。
In the function body, you can use the return keyword at any time to end the function's execution and return a value of an appropriate type. This is also the approach closest to most developers' experience:
Example
return a + b;
}
However, Rust does not support automatic return type inference! If the return type of the function is not explicitly declared, the function will be considered a "pure procedure" and is not allowed to produce a return value. The return keyword cannot be followed by a return value expression. The purpose of this is to allow public functions to form a visible bulletin.
Note:A function body expression is not equivalent to a function body; it cannot usereturnthe keyword.
Other Extensions