变量
Rust是强类型语言。
强类型语言,即强制类型定义的编程语言,也就是要求所有变量都必须先定义后使用。
在 VSCode 编辑器中, let x = "abcde";
然后 println(Value of x is {},x)
,编译时发现可以通过。
没有指定类型呀? 原来编辑器可以自动判断变量类型,然后在编译前按默认类型指定,再进行编译。 我又用windows的命令提示符CMD测试了一下,发现也能编译通过。 所以应该是编译器编译前自动优化。
//简单举例
fn main(){
let x:u64 = 75;
// 这里声明变量 x 是无符号 64 位 整型变量
// 如果没有指定变量 x 的类型,就会自动判定为默认类型 i32
// 这样会影响变量 x 的取值范围,具体看数据类型部分。
}
变量遮蔽 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。
fn main() {
let x = 3; //声明变量为i32整型的数字 3
let x = x + 5; //声明变量为i32整型的数字 8,用到了前一个x的值
let x = x * 3; //声明变量为i32整型的数字 24,用到了前一个x的值
println!("The value of x is: {}", x);
}
fn main(){
let x = "abcde"; //声明变量为&str类型
let x = "abc"; //声明变量为&str类型,没有用前一个x的值
println!("{}",x) //abc
}
// 正常输出abc,但是有一个warning:unused variable: `x`
// 并且指向 let x = "abcde"中的 x:
// help: if this is intentional, prefix it with an underscore: `_x`
// 如果给变量 x 加上下划线_前缀,就不会有这个warning了,只是输出后一个变量"abc"
// 如果仅声明一个变量x,加下划线前缀,会怎么样
fn main(){
let _x = "abcde";
println!("{}",x); //很遗憾,无法编译
}
// 这次没有warning了,但是给出了一个error
// error指向了println!("{}",x)中的 x:
// help: a local variable with a similar name exists: `_x`
// error: aborting due to previous error
// For more information about this error, try `rustc --explain E0425`.
// error: could not compile project.
// 关于下划线_前缀的问题,放到下面解决。
下划线 _ underline
变量遮蔽中,已经声明要给变量,重新声明同名变量时,如果没有用到前一个变量的值,就会提示warning,警告有个变量没有使用,提示如果有意如此,应给前一个未使用的变量加前缀,以忽略这个变量。
其实通过let声明新变量,名字和前一个同名,但是在不同的内存中。变量遮蔽用于特殊场合,所以一般情况下,如非必要,普通变量不要有同名变量最好,其次下划线也不要用在普通变量前缀。
下划线就好像表示后面用不到这个变量的,但是用不到为什么要声明呢?
//当用下划线做变量名时,无法编译。
fn main() {
let _ = "hello, Rust!";
println!("{}",_);
}
// note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
// error: in expressions, `_` can only be used on the left-hand side of an assignment
// `_` not allowed here
// could not compile `playground`
当用下划线做变量名前缀时, _arg
,提示有局部变量 _arg 。
fn main() {
let _arg = "hello, Rust!";
println!("{}",arg);
}
// 输出错误:
// a local variable with a similar name exists: `_arg`
// error: aborting due to previous error
// For more information about this error, try `rustc --explain E0425`.
// error: could not compile `playground`
多值赋值
fn main() {
let x = (10,20,30);
let (a1,a2,a3) = x;
println!("{:?},{:?},{:?}",a1,a2,a3);
}
//10,20,30
fn main() {
let x = true;
let y = !x;
println!("{},{}",x,y);
}
//true,false
常量
- 常量声明关键字:const;
- 常量名要用大写,如果不用大写,编译时提示错误;
- 常量后面必须有类型后缀;
- 非常量的变量名如果全部大写,会警告应改为小写,但是能编译成功。
- 语法为:
const PI:f64 = 3.1415926;
let 虽然可以将类型后缀到值后面,但是常量不行。否则会提示:
provide a type for the constant:
PI: f64``const PI = 3.1415f64;
println!("{}",PI);
//提示错误如下:
//error: missing type for `const` item
// --> main.rs:2:10
// |
//2 | const PI = 3.1415f64;
// | ^^ help: provide a type for the constant: `PI: f64`
//
//error: aborting due to previous error
常量不能遮蔽,尤其是后面声明的常量不能遮蔽前面声明的常量
- 常量作用域:常量仅在它所在的 块内有效
{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
.
```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);
}
// 3.1415 局部块作用域
// 3.14 全局
// 3 局部块作用域
// 3.14 全局
- 常量必须刚开始就明确值和类型,不能在运行时确定值, ```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
.
```