本文章内容基于以下环境,如若出入请参考当前环境。git
rustc 1.42.0 (b8cedc004 2020-03-09)
cargo 1.42.0 (86334295e 2020-01-31)
可变变量、不可变量、常量 是rust语言的一个特性。本篇文章主要讲rust的三个关键字:let
、mut
、const
github
let
是建立变量的关键字,rust默认建立的变量为不可变量。吐槽:大多数状况下,程序都是使用可变变量,而rust却默认建立的不可变的变量,纯粹为了特别而特别吧!ide
//设置不可变量 let x = 5; println!("let x={}",x);
不可变量不可对变量进行修改,错误示例函数
//设置不可变量 let x = 5; println!("let x={}",x); //不能够对不可变量进行修改 x = 6; println!("let x={}",x);
编译错误学习
error[E0384]: cannot assign twice to immutable variable `x` --> main.rs:6:2 | 3 | let x = 5; | - | | | first assignment to `x` | help: make this binding mutable: `mut x` ... 6 | x = 6; | ^^^^^ cannot assign twice to immutable variable error: aborting due to previous error For more information about this error, try `rustc --explain E0384`.
不可变量能够从新定义,从新定义包括改变类型this
正确示例spa
//设置不可变量 let x = 5; println!("let x={}",x); //从新定义 let x = 6; println!("let x={}",x); //从新定义,并改变类型 let x = "字符串"; println!("let x={}",x);
运行结果code
let x=5 let x=6 let x=字符串
绝大多数状况下,程序都是定义为能够修改的变量,变量原本就是要变的,只是rust加了一层不可变而已。rust下须要加上mut
才能使变量能够修改。orm
正确示例ip
//设置可变量 let mut x = 5; println!("let x={}",x); //修改值 x = 6; println!("let x={}",x);
运行结果
let x=5 let x=6
修改值须要保持类型一致,错误示例
//设置不可变量 let mut x = 5; println!("let x={}",x); //修改值 x = "字符串"; println!("let x={}",x);
编译错误
error[E0308]: mismatched types --> main.rs:6:6 | 6 | x = "字符串"; | ^^^^^^^^ expected integer, found `&str` error: aborting due to previous error For more information about this error, try `rustc --explain E0308`.
定义常量使用const
做为关键字。
//设置常量 const PI:f32 = 3.14; println!("const PI={}",PI);
运行结果
const PI=3.14
常量不可修改,这跟不可变量是同样的
常量不可从新定义,错误示例1
fn main(){ //设置常量 const PI:f32 = 3.14; println!("const PI={}",PI); const PI:f32 = 3.1415; println!("const PI={}",PI); }
编译错误
error[E0428]: the name `PI` is defined multiple times --> main.rs:5:2 | 3 | const PI:f32 = 3.14; | -------------------- previous definition of the value `PI` here 4 | println!("const PI={}",PI); 5 | const PI:f32 = 3.1415; | ^^^^^^^^^^^^^^^^^^^^^^ `PI` redefined here | = note: `PI` must be defined only once in the value namespace of this block error: aborting due to previous error For more information about this error, try `rustc --explain E0428`.
错误示例2
//设置常量 const PI:f32 = 3.14; println!("const PI={}",PI); let PI:f32 = 3.1415; println!("let PI={}",PI);
编译错误
warning: floating-point types cannot be used in patterns --> main.rs:5:6 | 5 | let PI:f32 = 3.1415; | ^^ | = note: `#[warn(illegal_floating_point_literal_pattern)]` on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620> error[E0005]: refutable pattern in local binding: `_` not covered --> main.rs:5:6 | 3 | const PI:f32 = 3.14; | -------------------- constant defined here 4 | println!("const PI={}",PI); 5 | let PI:f32 = 3.1415; | ^^ | | | interpreted as a constant pattern, not a new variable | help: introduce a variable instead: `pi_var` warning: floating-point types cannot be used in patterns --> main.rs:5:6 | 5 | let PI:f32 = 3.1415; | ^^ | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620> error: aborting due to previous error For more information about this error, try `rustc --explain E0005`.
常量不能推导类型须要肯定类型,必须明确类型。吐槽:推导类型应该不难吧,纯粹就是为了瘸一班的感受!
错误示例
//设置常量 const PI = 3.14; println!("const PI={}",PI);
编译错误
error: missing type for `const` item --> main.rs:3:11 | 3 | const PI = 3.14; | ^^ help: provide a type for the item: `PI: f64` error: aborting due to previous error
常量能够在函数外部定义,而可变量和不可变量不能够。
正确示例
//设置常量 const PI:f32 = 3.14; fn main(){ println!("const PI={}",PI); }
运行结果
const PI=3.14
错误示例
//设置 let PI:f32 = 3.14; fn main(){ println!("const PI={}",PI); }
编译错误
error: expected item, found keyword `let` --> main.rs:2:1 | 2 | let PI:f32 = 3.14; | ^^^ expected item error: aborting due to previous error
rust分为可变变量、不可变变量、常量,默认建立的变量为不可变量。