普通人學習rust——從零到放棄 變量、不可變量、常量

普通人學習rust——從零到放棄 變量、不可變量、常量

環境

本文章內容基於以下環境,如若出入請參考當前環境。git

rustc 1.42.0 (b8cedc004 2020-03-09)
cargo 1.42.0 (86334295e 2020-01-31)

前言

可變變量、不可變量、常量 是rust語言的一個特性。本篇文章主要講rust的三個關鍵字:letmutconstgithub

不可變量

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分爲可變變量、不可變變量、常量,默認建立的變量爲不可變量。

相關文章
相關標籤/搜索