在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
2.3 函数[root@itoracle src]# cargo new functions
Created binary (application) `functions` package
[root@itoracle src]# cd functions/
[root@itoracle functions]# vim src/main.rs
Rust code uses snake case as the conventional style for function and variable names. In snake case, all letters are lowercase and underscores separate words. Here’s a program that contains an example function definition: fn main() {
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Another function.");
}
Rust doesn’t care where you define your functions, only that they’re defined somewhere. [root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/goapp/src/functions)
Finished dev [unoptimized + debuginfo] target(s) in 6.41s
Running `target/debug/functions`
Hello, world!
Another function.
函数参数In function signatures, you must declare the type of each parameter. This is a deliberate decision in Rust’s design: requiring type annotations in function definitions means the compiler almost never needs you to use them elsewhere in the code to figure out what you mean. 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);
}
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/goapp/src/functions)
Finished dev [unoptimized + debuginfo] target(s) in 3.03s
Running `target/debug/functions`
The value of x is: 5
The value of y is: 6
函数体Function bodies are made up of a series of statements optionally ending in an expression. We’ve actually already used statements and expressions. Statements are instructions that perform some action and do not return a value. Expressions evaluate to a resulting value. Let’s look at some examples. Creating a variable and assigning a value to it with the fn main() {
let y = 6;
}
Function definitions are also statements; the entire preceding example is a statement in itself. Statements do not return values. Therefore, you can’t assign a fn main() {
let x = (let y = 6);
}
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/goapp/src/functions)
error: expected expression, found statement (`let`)
--> src/main.rs:2:14
|
2 | let x = (let y = 6);
| ^^^ expected expression
|
= note: variable declaration using `let` is a statement
error: aborting due to previous error
error: Could not compile `functions`.
To learn more, run the command again with --verbose.
The Expressions evaluate to something and make up most of the rest of the code that you’ll write in Rust. Consider a simple math operation, such as fn main() {
let _x = 5;
let _y = {
let _x = 3;
_x + 1
};
println!("The value of y is: {}", _y);
}
let y = {
let x = 3;
x + 1
};
is a block that, in this case, evaluates to [root@itoracle functions]# cargo run src/main.rs
Compiling functions v0.1.0 (/usr/local/automng/src/goapp/src/functions)
Finished dev [unoptimized + debuginfo] target(s) in 0.63s
Running `target/debug/functions src/main.rs`
The value of y is: 4
函数返回值We don’t name return values, but we do declare their type after an arrow ( fn five() -> i32 {
5
}
fn main() {
let x = five();
println!("The value of x is: {}", x);
}
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions)
Finished dev [unoptimized + debuginfo] target(s) in 4.96s
Running `target/debug/functio
There are two important bits: first, the line let x = 5; Second, the fn main() {
let x = plus_one(5);
println!("The value of x is: {}", x);
}
fn plus_one(x: i32) -> i32 {
x + 1
}
注意,函数体结束的时候没有分号“;” [root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions)
Finished dev [unoptimized + debuginfo] target(s) in 1.76s
Running `target/debug/functions`
The value of x is: 6
如果不是在函数体最后一行返回,则可以使用return语句 fn main() {
let mut x = plus_one(5);
println!("The value of x is: {}", x);
x = plus_one(15);
println!("The value of x is: {}", x);
}
fn plus_one(mut x: i32) -> i32 {
if x < 10 {
return 10;
}
x = x + 10;
x
}
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions)
Finished dev [unoptimized + debuginfo] target(s) in 1.22s
Running `target/debug/functions`
The value of x is: 10
The value of x is: 25
另外,最后一句使用return语句也是可以的,但rust建议使用不加分号的表达式 fn main() {
let mut x = plus_one(5);
println!("The value of x is: {}", x);
x = plus_one(15);
println!("The value of x is: {}", x);
}
fn plus_one(mut x: i32) -> i32 {
if x < 10 {
return 10;
}
x = x + 10;
return x;
}
[root@itoracle functions]# cargo run
Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions)
Finished dev [unoptimized + debuginfo] target(s) in 2.43s
Running `target/debug/functions`
The value of x is: 10
The value of x is: 25
没有返回值的函数 fn justgo(){ println!("永远是多远?"); } [root@itoracle functions]# cat src/main.rs fn main() { let mut x = plus_one(5); println!("The value of x is: {}", x); x = plus_one(15); println!("The value of x is: {}", x); justgo(); } fn plus_one(mut x: i32) -> i32 { if x < 10 { return 10; } x = x + 10; x } fn justgo(){ println!("永远是多远?"); } 运行结果 [root@itoracle functions]# cargo run Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions) Finished dev [unoptimized + debuginfo] target(s) in 4.95s Running `target/debug/functions` The value of x is: 10 The value of x is: 25 永远是多远?
函数定义后,必须被调用,不然运行时会给出警告 warning: function is never used: `justgo` --> src/main.rs:19:1 | 19 | fn justgo(){ | ^^^^^^^^^^^ Finished dev [unoptimized + debuginfo] target(s) in 5.48s Running `target/debug/functions`
如果不想一直看到该warning,可以在函数所在的文件第一行加入 #![allow(unused)]
使用元组让函数返回多个值 fn testFnReturn() -> (u8,String){ (1,"good".to_string()) } let a = testFnReturn(); println!("{}",a.1);
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论