变量
- 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[E0384]: 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 [unoptimized + debuginfo] 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[E0308]: 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
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |