变量

Rust provides type safety via static typing. Variable bindings are made with let:

fn main() {
    let x: i32 = 10;
    println!("x: {x}");
    // x = 20;
    // println!("x: {x}");
}
This slide should take about 5 minutes.
  • 取消备注 x = 20,以证明变量默认是不可变的。添加 mut 关键字以允许进行更改。

  • Warnings are enabled for this slide, such as for unused variables or unnecessary mut. These are omitted in most slides to avoid distracting warnings. Try removing the mutation but leaving the mut keyword in place.

  • 这里的 i32 是变量的类型。编译时必须已知类型,但在很多情况下,由于具有类型推理功能(稍后介绍),程序员可以忽略这一点。