Rust Functions
Functions are ubiquitous in the Rust language.
From the previous chapters you can already understand the basic form of a Rust function:
fn <函数名> ( <参数> ) <函数体>
The naming style of Rust function names is lowercase letters separated by underscores:
Example
fn
main
(
)
{
println
!
(
"Hello, world!"
)
;
another_function
(
)
;
}
fn
another_function
(
)
{
println
!
(
"Hello, runoob!"
)
;
}
Running result:
Hello, world!
Hello, runoob!
Note that we defined another_function after the main function in the source code. Rust does not care where you define functions, as long as they are defined somewhere.
Function parameters
If a function defined in Rust 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
)
;
}
Running result:
x 的值为 : 5
y 的值为 : 6
Statements and expressions in a function body
A Rust function body consists of a series of statements (Statement) that may 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 a computation step and a return value. The following are expressions (assuming the identifiers shown have been defined):
a = 7
b + 2
c * (a + b)
In Rust, you can write a more complex expression in 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
)
;
}
Running result:
x 的值为 : 5
y 的值为 : 4
Obviously, this program contains an expression block:
{
let x = 3;
x + 1
};
And you can use function statements in the block; the last step is an expression, and the result value of this expression is the value represented by the whole expression block. Such an expression block is called a function-body expression.
Note: there is no semicolon after x + 1, otherwise it would become a statement!
Such an expression block is a valid function body. And in Rust, function definitions can be nested:
Example
fn
main
(
)
{
fn
five
(
)
->
i32
{
5
}
println
!
(
"The value of five() is: {}"
,
five
(
)
)
;
}
Function return value
The previous nested example showed how a Rust function declares its return type: after the parameter declaration, use -> to declare the function's return type (not : ).
In the function body, you can at any time end the function with the return keyword and return a value of the appropriate type. This is also the approach closest to most developers' experience:
Example
fn
add
(
a
:
i32
,
b
:
i32
)
->
i32
{
return
a
b
;
}
But Rust does not support automatic return type inference! If the return type is not explicitly declared, the function is considered a "pure procedure", not allowed to produce a return value, and return cannot be followed by a return expression. The purpose of this is to allow public functions to form a visible contract.
Note:A function-body expression is not equivalent to a function body; it cannot use return The const keyword.