函数的三种类型

  • 有参数有返回值
  • 有参数无返回值
  • 无参数无返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn other_fun() {
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;
//c语言常用这种写法返回,但是Rust使用下面的写法较多
//return result;
result//或者直接使用a+b也可以
}

fn main() {
other_fun();
let a: i32 = -1;
let b: u32 = 20;
let c: i32 = 6;
other_fun1(a,b);
let r: i32 = other_fun2(a,c);
println!("r = {} ",r );
}