庾芷秋 发表于 2025-6-5 14:43:07

变量与可变性

变量

fn main() {
    let mut x = 5;
    println!("The value of x is: {x}");
    x = 6;
    println!("The value of x is: {x}");
}使用 let 可以申明一个不可变的变量,变量默认是不可改变的(immutable)
如果要申明一个可变的变量,使用 let mut 来申明
如果对不可变变量重新赋值,使用 cargo run 会看到错误信息,输出如下
$ cargo run
   Compiling variables v0.1.0 (file:///projects/variables)
error: cannot assign twice to immutable variable `x`
--> src/main.rs:4:5
|
2 |   let x = 5;
|         -
|         |
|         first assignment to `x`
|         help: consider making this binding mutable: `mut x`
3 |   println!("The value of x is: {x}");
4 |   x = 6;
|   ^^^^^ cannot assign twice to immutable variable

For more information about this error, try `rustc --explain E0384`.
error: could not compile `variables` (bin "variables") due to 1 previous error错误信息指出错误的原因是 不能对不可变变量 x 二次赋值 (cannot assign twice to immutable variable 'x' ) ,因为你尝试对不可变变量 x 赋第二个值。
常量

常量 (constants) 是绑定到一个名称的不允许改变的值
不允许对常量使用 mut
声明常量使用 const 关键字而不是 let ,并且必须注明值的类型
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;Rust 对常量的命名约定是在单词之间使用全大写加下划线
在声明它的作用域之中,常量在整个程序生命周期中都有效
隐藏(Shadowing)

定义一个变量后,可以再定义一个与之同名的变量,Rustacean 们称之为第一个变量被第二个 **隐藏(Shadowing) **了,当使用变量的名称时,编译器将看到第二个变量。
ForExample:
fn main() {
    let x = 5;

    let x = x + 1;

    {
      let x = x * 2;
      println!("The value of x in the inner scope is: {x}");
    }

    println!("The value of x is: {x}");
}执行命令 cargo run 可以在控制台看到输出:
$ cargo run
   Compiling variables v0.1.0 (file:///projects/variables)
    Finished `dev` profile target(s) in 0.31s
   Running `target/debug/variables`
The value of x in the inner scope is: 12
The value of x is: 6这个程序首先将 x 绑定到值 5 上。接着通过 let x = 创建了一个新变量 x,获取初始值并加 1,这样 x 的值就变成 6 了。然后,在使用花括号创建的内部作用域内,第三个 let 语句也隐藏了 x 并创建了一个新的变量,将之前的值乘以 2,x 得到的值是 12。当该作用域结束时,内部 shadowing 的作用域也结束了,x 又返回到 6。
mut 与隐藏的另一个区别是,当再次使用 let 时,实际上创建了一个新变量,我们可以改变值的类型,并且复用这个名字。例如,假设程序请求用户输入空格字符来说明希望在文本之间显示多少个空格:
let spaces = "   ";
let spaces = spaces.len();第一个 spaces 变量是字符串类型,第二个 spaces 变量是数字类型。隐藏使我们不必使用不同的名字,如 spaces_str 和 spaces_num;相反,我们可以复用 spaces 这个更简单的名字。然而,如果尝试使用 mut,将会得到一个编译时错误,如下所示:
let mut spaces = "   ";
spaces = spaces.len();这个错误说明,我们不能改变变量的类型:
$ cargo run
   Compiling variables v0.1.0 (file:///projects/variables)
error: mismatched types
--> src/main.rs:3:14
|
2 |   let mut spaces = "   ";
|                      ----- expected due to this value
3 |   spaces = spaces.len();
|            ^^^^^^^^^^^^ expected `&str`, found `usize`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `variables` (bin "variables") due to 1 previous error
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 变量与可变性