None
小于 1 分钟
Math Is Life
trait
;
结尾避免悬垂引用
let r;
{
let x = 1;
r = &x;
}
println!("{}", x)
一点信息
这一章的选项实在是太难搞了,我干脆直接截图
声明方法
fn main(){
print!("hi,rust!");
function_name();
}
fn function_name(){
functiom_body;
}
函数参数:
fn main(){
function(5, 6);//实参
}
fn function(x: i32, y: u32){//形参
print!("The x is {}, the y is {}", x, y);
}
Stack 按值的接受顺序来储存,按相反的顺序将它们移除(后进先出 LIFO,LastIn FirstOut)
所有储存在 Stack 上的出局必须拥有固定一致的大小
Heap 内存组织性差一些
在用
,并返回一个指针,也就是这个内存的空间地址分配
存储数据
访问数据
函数调用: 当你的代码调用函数时,值被传入函数(也包括指向 heap 的指针). 函数本地的变量被压到 Stack 上. 当函数结束后,这些值会从 Stack 上弹出
else
表达式else if
,那么最好使用match
来重构代码let condition = true;
let number = if condition {5} else {6};//此时number被赋值 5
//let number = if condition {5} else {"6"};
//这样是错误的,rust必须知道number的确切类型