Variable and Mutability
let
declares a variable. This is immutable by default. If you want to make it mutable, add mut
keyword:
let x = 5;
let mut y = 6;
x = 7; //Compile Error
y = 7; //No error
const
creates a constant. This can never change, and must be set to a constant expression, not something that has to be determined at runtime.
const MAX_POINTS: u32 = 100_000;
Shadowing is when you declare a new variable with the same name as a previous one. (The first is shadowed by the second variable.) You shadow by using let
repeatedly:
let x = 5;
let x = 7;
let x = "howdy";
This is different from mut
because the latter doesn't use let
, and because of this shadowing allows us to create the variable with a different type. This wouldn't work:
let mut x = 5;
x = 7; //No error
x = "howdy"; //Error