变量与可变性

守听  金牌会员 | 2024-10-15 18:21:17 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 876|帖子 876|积分 2628

变量
  1. fn main() {
  2.     let mut x = 5;
  3.     println!("The value of x is: {x}");
  4.     x = 6;
  5.     println!("The value of x is: {x}");
  6. }
复制代码
使用 let 可以申明一个不可变的变量,变量默认是不可改变的(immutable)
如果要申明一个可变的变量,使用 let mut 来申明
如果对不可变变量重新赋值,使用 cargo run 会看到错误信息,输出如下
  1. $ cargo run
  2.    Compiling variables v0.1.0 (file:///projects/variables)
  3. error[E0384]: cannot assign twice to immutable variable `x`
  4. --> src/main.rs:4:5
  5.   |
  6. 2 |     let x = 5;
  7.   |         -
  8.   |         |
  9.   |         first assignment to `x`
  10.   |         help: consider making this binding mutable: `mut x`
  11. 3 |     println!("The value of x is: {x}");
  12. 4 |     x = 6;
  13.   |     ^^^^^ cannot assign twice to immutable variable
  14. For more information about this error, try `rustc --explain E0384`.
  15. 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 ,并且必须注明值的范例
  1. const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
复制代码
Rust 对常量的定名约定是在单词之间使用全大写加下划线
在声明它的作用域之中,常量在整个步伐生命周期中都有用
隐藏(Shadowing)

界说一个变量后,可以再界说一个与之同名的变量,Rustacean 们称之为第一个变量被第二个 **隐藏(Shadowing) **了,当使用变量的名称时,编译器将看到第二个变量。
ForExample:
  1. fn main() {
  2.     let x = 5;
  3.     let x = x + 1;
  4.     {
  5.         let x = x * 2;
  6.         println!("The value of x in the inner scope is: {x}");
  7.     }
  8.     println!("The value of x is: {x}");
  9. }
复制代码
实行命令 cargo run 可以在控制台看到输出:
  1. $ cargo run
  2.    Compiling variables v0.1.0 (file:///projects/variables)
  3.     Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s
  4.      Running `target/debug/variables`
  5. The value of x in the inner scope is: 12
  6. The value of x is: 6
复制代码
这个步伐首先将 x 绑定到值 5 上。接着通过 let x = 创建了一个新变量 x,获取初始值并加 1,如许 x 的值就变成 6 了。然后,在使用花括号创建的内部作用域内,第三个 let 语句也隐藏了 x 并创建了一个新的变量,将之前的值乘以 2,x 得到的值是 12。当该作用域结束时,内部 shadowing 的作用域也结束了,x 又返回到 6。
mut 与隐藏的另一个区别是,当再次使用 let 时,现实上创建了一个新变量,我们可以改变值的范例,并且复用这个名字。比方,假设步伐请求用户输入空格字符来说明希望在文本之间表现多少个空格:
  1. let spaces = "   ";
  2. let spaces = spaces.len();
复制代码
第一个 spaces 变量是字符串范例,第二个 spaces 变量是数字范例。隐藏使我们不必使用差别的名字,如 spaces_str 和 spaces_num;相反,我们可以复用 spaces 这个更简单的名字。然而,如果实验使用 mut,将会得到一个编译时错误,如下所示:
  1. let mut spaces = "   ";
  2. spaces = spaces.len();
复制代码
这个错误说明,我们不能改变变量的范例:
  1. $ cargo run
  2.    Compiling variables v0.1.0 (file:///projects/variables)
  3. error[E0308]: mismatched types
  4. --> src/main.rs:3:14
  5.   |
  6. 2 |     let mut spaces = "   ";
  7.   |                      ----- expected due to this value
  8. 3 |     spaces = spaces.len();
  9.   |              ^^^^^^^^^^^^ expected `&str`, found `usize`
  10. For more information about this error, try `rustc --explain E0308`.
  11. error: could not compile `variables` (bin "variables") due to 1 previous error
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

守听

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表