7.8. Rust function

发布时间 :2023-11-06 23:00:08 UTC      

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:

7.8.1. 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:

7.8.2. 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:

7.8.3. 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:

7.8.4. 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:

7.8.5. 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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.