头条地址:https://www.ixigua.com/i6765442674582356483
B站地址:https://www.bilibili.com/video/av78062009?p=1
网易云课堂地址:https://study.163.com/course/introduction.htm?courseId=1209596906#/courseDetail?tab=1
//1、rust中函数和变量名都使用snake case规范风格,否则会有一个警告
//rust不关注函数定义的位置,只要定义了就可以
fn another_fun() {
println!("This is a function");
}
//2、函数参数
fn another_fun1(x: u32) {
println!("x = {}", x);
}
fn another_fun2(x: u32, y:u32) {
println!("x = {}, y = {}", x, y);
}
fn main() {
another_fun();
another_fun1(1);
another_fun2(1, 2);
//3、语句是执行一些操作,但不返回值的指令
let y = 1;//这是一个语句,不返回值
//let x = (let y = 1); //要报错,因为语句不返回值
//4、表达式会计算出一些值
let y = {
let x = 3;
x + 1 //注意:不能加逗号
};
println!("y = {}", y);
let y = add_two_pra(1, 2);
println!("y = {}", y);
println!("Hello, world!");
}
//5、带返回值的函数
fn add_two_pra(x:u32, y:u32) ->u32 {
x + y
//x + y; //错误,返回值不能带逗号
}