Rust function


Release date:2023-11-07 Update date:2023-11-07 Editor:admin View counts:189

Label:

Rust function

Functions are ubiquitous in Rust.

You can learn the basic form of the Rust function from the previous chapter:

fn<function name>(<parameter>)<function body>

The naming style of Rust function name is lowercase underscore:

Example

fn main() {
    println!("Hello, world!");
    another_function();
}
fn another_function() {
    println!("Hello, runoob!");
}

Running result:

Hello, world!
Hello, runoob!

Note that we have defined another_function after the main function in the source code function. Rust doesn’t care where you define functions, just define them somewhere.

Function parameter

The function defined in Rust must declare the parameter name and type if it needs to have parameters:

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:

The value of x is : 5
The value of y is : 6

Statements and expressions of the function body

The body of a Rust function consists of a series of statements (Statement) that can end with an expression. So far, we have only seen functions that do not end with an expression, but have used the expression as part of the statement.

Statement is a step that performs some action and does not return a value. For example:

let a = 6;

There is no return value for this step, so the following statement is incorrect:

let a = (let b = 2);

The expression has an evaluation step and a return value. The following is the expression (assuming the identifier that appears has been defined):

a = 7
b + 2
c * (a + b)

In Rust, you can use a {} to write a more complex expression in the included block:

Example

fn main() {
    let x = 5;
    let y = {
        let x = 3;
        x + 1
    };
    println!("The value of x is : {}", x);
    println!("The value of x is is : {}", y);
}

Running result:

The value of x is : 5
The value of y is : 4

Obviously, this program contains an expression block:

{
    let x = 3;
    x + 1
};

And you can use function statements in the block, and the final step is the expression, whose resulting value is the value represented by the entire expression block. This expression block is called a function body expression.

Note: there is no semicolon after x + 1, otherwise it will become a statement!

This expression block is a legal function body. And in Rust, function definitions can be nested:

Example

fn main() {
    fn five() -> i32 {
        5
    }
    println!("five() the value is : {}", five());
}

Function return value

In the previous nested example, you have shown how the Rust function declares the return value type: use-> to declare the type of the function return value (not:) after the parameter declaration.

In the body of a function, you can use the return keyword ends the function running and returns a value of the appropriate type. This is also closest to the experience of most developers:

Example

fn add(a: i32, b: i32) -> i32 {
    return a + b;
}

But Rust does not support automatic return value type judgment! If the type of the return value of the function is not explicitly declared, the functionwill be considered a “pure procedure” and the return value is not allowedto be generated. return cannot be followed by a return value expression. The purpose of this is to enable the exposed function to form a visible bulletin.

Note: a function body expression cannot be equated with a function body, andit cannot be used return keyword.

Powered by TorCMS (https://github.com/bukun/TorCMS).