fnmain() { let y : i64 = 10; // 設定變數y型別為整數型64位元, 值為10 println!("The value of y is {}", y) // 印出y }
Output:
1 2 3 4 5
$ cargo run Compiling rust_project v0.1.0 (D:\000_git_repo_projects\rust_project) Finished dev [unoptimized + debuginfo] target(s) in 0.50s Running `target\debug\rust_project.exe` The value of y is 10
2. 變數重複賦值
Compiler 直接拋出錯誤, 不可重複對不可變(不可改變值的變數)的變數賦值 範例 1-2
Sample Code 2-1 重複賦值, compiler報錯
1 2 3 4 5 6
fnmain() { let y : i64 = 10; // 設定變數y型別為整數型64位元, 值為10 println!("The value of y is {}", y) // 印出y y = 20; println!("The new value of y is {}", y) }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14
error[E0384]: cannot assign twice to immutable variable `y` --> src\main.rs:4:5 | 2 | let y : i64 = 10; // 設定變數y型別為整數型64位元, 值為10 | - | | | first assignment to `y` | help: consider making this binding mutable: `mut y` 3 | println!("The value of y is {}", y); // 印出y 4 | y = 20; | ^^^^^^ cannot assign twice to immutable variable
For more information about this error, try `rustc --explain E0384`. error: could not compile `rust_project` due to previous error
看 terminal 輸出的訊息, 可把變數 y 的型態設定為 mut (mutable) 可變
Sample Code 2-2 指定為可變變數 mut
1 2 3 4 5 6
fnmain() { letmut y : i64 = 10; // 設定變數y型別為整數型64位元, 值為10 println!("The value of y is {}", y); // 印出y y = 20; println!("The new value of y is {}", y); }
Output
1 2 3 4 5 6
$ cargo run Compiling rust_project v0.1.0 (D:\000_git_repo_projects\rust_project) Finished dev [unoptimized + debuginfo] target(s) in 0.37s Running `target\debug\rust_project.exe` The value of y is 10 The new value of y is 20
fnmain() { let y : i64 = 10; // 設定變數y型別為整數型64位元, 值為10 println!("The value of y is {}", y); // 印出y = 10 let y : i64 = 20; // 重新賦值, 覆蓋過heap堆上的值, 重新取代 println!("The new value of y is {}", y); // 印出y = 20 }
Output
1 2 3 4 5
$ cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target\debug\rust_project.exe` The value of y is 10 The new value of y is 20
compiler 沒有報錯, 因為 y 重新 被賦值
此時如果指定 y 為 mut 型態, compiler 就會出現善意提醒
Sample Code 3-2 提醒mut為非必要
1 2 3 4 5 6
fnmain() { letmut y : i64 = 10; // 設定變數y型別為可變整數型64位元, 值為10 println!("The value of y is {}", y); // 印出y = 10 let y : i64 = 20; // 重新賦值, 覆蓋過heap堆上的值, 重新取代 println!("The new value of y is {}", y); // 印出y = 20 }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
warning: variable does not need to be mutable --> src\main.rs:2:9 | 2 | let mut y : i64 = 10; // 設定變數y型別為可變整數型64位元, 值為10 | ----^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default
warning: `rust_project` (bin "rust_project") generated 1 warning Finished dev [unoptimized + debuginfo] target(s) in 0.39s Running `target\debug\rust_project.exe` The value of y is 10 The new value of y is 20
fnmain() { const y : i64 = 10; // 設定常數y型別為整數型64位元, 值為10 println!("The value of y is {}", y); // 印出y = 10 }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13
Compiling rust_project v0.1.0 (D:\000_git_repo_projects\rust_project) warning: constant `y` should have an upper case name --> src\main.rs:2:11 | 2 | const y : i64 = 10; // 設定變數y型別為可變整數型64位元, 值為10 | ^ help: convert the identifier to upper case (notice the capitalization): `Y` | = note: `#[warn(non_upper_case_globals)]` on by default
warning: `rust_project` (bin "rust_project") generated 1 warning Finished dev [unoptimized + debuginfo] target(s) in 0.39s Running `target\debug\rust_project.exe` The value of y is 10
Compiler 提醒需要大寫, 但仍正常編譯
對常數重新賦值, compiler 報錯
Sample Code 對常數重新賦值, 報錯
1 2 3 4 5 6
fnmain() { const Y : i64 = 10; // 設定常數y型別為整數型64位元, 值為10 println!("The value of y is {}", Y); // 印出y = 10 let Y : i64 = 20; // 重新賦值 報錯 println!("{}", Y) }
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
error[E0005]: refutable pattern inlocal binding: `i64::MIN..=9_i64` and `11_i64..=i64::MAX` not covered --> src\main.rs:4:9 | 2 | const Y : i64 = 10; // 設定常數y型別為整數型64位元, 值為10 | ------------------- constant defined here 3 | println!("The value of y is {}", Y); // 印出y = 10 4 | let Y : i64 = 20; | ^ | | | interpreted as a constant pattern, not a new variable | help: introduce a variable instead: `y_var` | = note: the matched value is of type `i64`
For more information about this error, try `rustc --explain E0005`. error: could not compile `rust_project` due to previous error