Immutable Variables vs Constant in RUST
Variables are immutable in Rust and so are constants. If so, why do we have them both and what is the purpose? Let's get into it.
Variables are by default immutable in RUST. The immutable nature of the variables can be changed by using the keyword "mut". But the constants remain the same throughout the life of the program.
let varA : u32 = 100 //The variable is immutable
let mut varB : u32 = 200 // This makes the variable mutable
const VARC : u32 = 300 // This is forever immutable
A point to note. During the reassignment of a mutable variable, the data type cannot be changed. For example, we cannot mutate a u32 variable with a string assignment
let mut x = 5;
? ? x="Apple";
This will throw the following compilation error:
x="Apple"
? |? ? ? ?^^^^^^^ expected integer, found `&str`;
Great Read!