Rust: Variables and Immutability
Status: Courses
-
Declare variables using
let#![allow(unused)] fn main() { // Declare variable with auto-assign type let var = 0; // Declare variable with defined type let var: i32 = 0 } -
By default, values in these variables cannot be changed or reassigned. So they are called immutable.
-
To declare variables that can change you need to use
mutkeyword#![allow(unused)] fn main() { // Declare variable with mut keyword let mut var = 0; } -
Variables with mut can change or re-assign their values.