变量

Rust是强类型语言。

强类型语言,即强制类型定义的编程语言,也就是要求所有变量都必须先定义后使用。

在 VSCode 编辑器中, let x = "abcde"; 然后 println(Value of x is {},x) ,编译时发现可以通过。

没有指定类型呀? 原来编辑器可以自动判断变量类型,然后在编译前按默认类型指定,再进行编译。 我又用windows的命令提示符CMD测试了一下,发现也能编译通过。 所以应该是编译器编译前自动优化。

  1. //简单举例
  2. fn main(){
  3. let x:u64 = 75;
  4. // 这里声明变量 x 是无符号 64 位 整型变量
  5. // 如果没有指定变量 x 的类型,就会自动判定为默认类型 i32
  6. // 这样会影响变量 x 的取值范围,具体看数据类型部分。
  7. }

变量遮蔽 shadowing

在《The Rust Programming Language》书中对于shadowing的说明是: We can shadow a variable by using the same variable’s name and repeating the use of the let keyword. 即使用let声明变量,会遮蔽前面的同名变量。
《Rust程序设计语言》一书中将shadowing翻译为 隐藏
菜鸟教程 中将shadowing翻译为 重影
在中英文术语对照表中,翻译为:变量遮蔽变量隐蔽变量隐藏变量覆盖
【《Rust编程 入门、实战与进阶》出版书中,翻译为 变量遮蔽
【《深入浅出Rust》出版书中,翻译为 变量遮蔽
所以这里也按 变量遮蔽 翻译 shadowing。

  1. fn main() {
  2. let x = 3; //声明变量为i32整型的数字 3
  3. let x = x + 5; //声明变量为i32整型的数字 8,用到了前一个x的值
  4. let x = x * 3; //声明变量为i32整型的数字 24,用到了前一个x的值
  5. println!("The value of x is: {}", x);
  6. }
  1. fn main(){
  2. let x = "abcde"; //声明变量为&str类型
  3. let x = "abc"; //声明变量为&str类型,没有用前一个x的值
  4. println!("{}",x) //abc
  5. }
  6. // 正常输出abc,但是有一个warning:unused variable: `x`
  7. // 并且指向 let x = "abcde"中的 x:
  8. // help: if this is intentional, prefix it with an underscore: `_x`
  9. // 如果给变量 x 加上下划线_前缀,就不会有这个warning了,只是输出后一个变量"abc"
  10. // 如果仅声明一个变量x,加下划线前缀,会怎么样
  11. fn main(){
  12. let _x = "abcde";
  13. println!("{}",x); //很遗憾,无法编译
  14. }
  15. // 这次没有warning了,但是给出了一个error
  16. // error指向了println!("{}",x)中的 x:
  17. // help: a local variable with a similar name exists: `_x`
  18. // error: aborting due to previous error
  19. // For more information about this error, try `rustc --explain E0425`.
  20. // error: could not compile project.
  21. // 关于下划线_前缀的问题,放到下面解决。

下划线 _ underline

变量遮蔽中,已经声明要给变量,重新声明同名变量时,如果没有用到前一个变量的值,就会提示warning,警告有个变量没有使用,提示如果有意如此,应给前一个未使用的变量加前缀,以忽略这个变量。

其实通过let声明新变量,名字和前一个同名,但是在不同的内存中。变量遮蔽用于特殊场合,所以一般情况下,如非必要,普通变量不要有同名变量最好,其次下划线也不要用在普通变量前缀。
下划线就好像表示后面用不到这个变量的,但是用不到为什么要声明呢?

  1. //当用下划线做变量名时,无法编译。
  2. fn main() {
  3. let _ = "hello, Rust!";
  4. println!("{}",_);
  5. }
  6. // note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
  7. // error: in expressions, `_` can only be used on the left-hand side of an assignment
  8. // `_` not allowed here
  9. // could not compile `playground`

当用下划线做变量名前缀时, _arg ,提示有局部变量 _arg 。

  1. fn main() {
  2. let _arg = "hello, Rust!";
  3. println!("{}",arg);
  4. }
  5. // 输出错误:
  6. // a local variable with a similar name exists: `_arg`
  7. // error: aborting due to previous error
  8. // For more information about this error, try `rustc --explain E0425`.
  9. // error: could not compile `playground`

多值赋值

  1. fn main() {
  2. let x = (10,20,30);
  3. let (a1,a2,a3) = x;
  4. println!("{:?},{:?},{:?}",a1,a2,a3);
  5. }
  6. //10,20,30
  1. fn main() {
  2. let x = true;
  3. let y = !x;
  4. println!("{},{}",x,y);
  5. }
  6. //true,false

常量

  1. 常量声明关键字:const;
  2. 常量名要用大写,如果不用大写,编译时提示错误;
  3. 常量后面必须有类型后缀;
  4. 非常量的变量名如果全部大写,会警告应改为小写,但是能编译成功。
  5. 语法为: const PI:f64 = 3.1415926;
  6. let 虽然可以将类型后缀到值后面,但是常量不行。否则会提示:provide a type for the constant:PI: f64``

    1. const PI = 3.1415f64;
    2. println!("{}",PI);
    3. //提示错误如下:
    4. //error: missing type for `const` item
    5. // --> main.rs:2:10
    6. // |
    7. //2 | const PI = 3.1415f64;
    8. // | ^^ help: provide a type for the constant: `PI: f64`
    9. //
    10. //error: aborting due to previous error
  7. 常量不能遮蔽,尤其是后面声明的常量不能遮蔽前面声明的常量

  8. 常量作用域:常量仅在它所在的 块内有效{const CONSTANT : <type> = VALUE; //CONST 仅在此大括号内有效} ```rust fn main(){ {const PI:f64= 3.1415; println!(“{}”,PI);} {const PI:f64= 3.14; println!(“{}”,PI);} {const PI:f64= 3.; println!(“{}”,PI);} println!(“{}”,PI); }

// error[E0425]: cannot find value PI in this scope // —> main.rs:5:18 // | // 5 | println!(“{}”,PI); // | ^^ not found in this scope // | // help: consider importing one of these items // | // 1 | use std::f32::consts::PI; // | // 1 | use std::f64::consts::PI; // |

// error: aborting due to previous error

// For more information about this error, try rustc --explain E0425.

  1. ```rust
  2. fn main(){
  3. {const PI:f64= 3.1415; println!("{}",PI);}
  4. const PI:f64= 3.14; println!("{}",PI);
  5. {const PI:f64= 3.; println!("{}",PI);}
  6. println!("{}",PI);
  7. }
  8. // 3.1415 局部块作用域
  9. // 3.14 全局
  10. // 3 局部块作用域
  11. // 3.14 全局
  1. 常量必须刚开始就明确值和类型,不能在运行时确定值, ```rust fn main(){ const PI:f64; PI = 3.14; println!(“{}”,PI); }

//error: free constant item without body 没有值的常量项 // —> main.rs:2:4 // | // 2 | const PI:f64; // | ^^^^^^^^^^^^- // | | // | help: provide a definition for the constant: = <expr>;

// error[E0070]: invalid left-hand side of assignment // —> main.rs:3:7 // | // 3 | PI = 3.14; // | — ^ // | | // | cannot assign to this expression

// error: aborting due to 2 previous errors

// For more information about this error, try rustc --explain E0070. ```