In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "what are the functions in the Rust language", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what are the functions in the Rust language" can help you solve your doubts? let's follow the Xiaobian's ideas to learn new knowledge.
Rust is a system programming language that focuses on security, especially concurrency security, and supports functional, imperative and generic programming paradigms. Rust is syntactically similar to C++, but the designers want to provide better memory security while ensuring performance.
Functions are ubiquitous in Rust.
You can learn the basic form of the Rust function from the previous chapter:
Fn ()
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!
Notice that we define another_function after the main function in the source code. 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! ("value of x is: {}", x); println! (value of "y is: {}", y);}
Running result:
The value of x is: 5y is the statement and expression of the body of 6 functions.
The body of a Rust function consists of a series of statements (Statement) that can end with an expression (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 = 7b + 2c * (a + b)
In Rust, you can write a more complex expression in a block included with {}:
Example
Fn main () {let x = 5; let y = {let x = 3; x + 1}; println! (value of "x is: {}", x); println! (value of "y is: {}", y);}
Running result:
The value of x is: 5y 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! (the value of "five () is: {}", five ();} function returns a 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.
At any time in the function body, you can end the function with the return keyword and return 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 function will be considered a "pure procedure", the return value is not allowed to be generated, and the 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, and it cannot use the return keyword.
After reading this, the article "what are the functions in Rust language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.