以下代码简单介绍了 Rust 中的函数
// 函数,遵循蛇形命名法
fn other_function() {
println!("This is a function.");
}
fn other_fun1(a: i32, b: u32) {
println!("a = {}, b = {}", a, b);
}
fn other_fun2(a: i32, b: i32) -> i32 {
let result = a + b;
return result;
}
fn other_fun3(a: i32, b: i32) -> i32 {
// let result = a + b;
// result
a + b
}
fn main() {
other_function();
let a: i32 = -1;
let b: u32 = 2;
other_fun1(a, b);
let c: i32 = 9;
let r: i32 = other_fun2(a, c);
println!("r = {}", r);
let r2: i32 = other_fun3(a, c);
println!("r2 = {}", r2);
// 语句是执行一些操作,但不返回值得指令
// let y = 1; // 语句,但不返回值
// let x = (let y = 1); // 会报错
// 表达式会计算一些值
let y = {
let x = 1;
x + 1
};
println!("y = {}", y);
}
执行 cargo r
,输出如下:
This is a function.
a = -1, b = 2
r = 8
r2 = 8
y = 2