Declare a variable in Rust and mutability
This is a common programming concepts. That might be not unique to rust. Let’s see how to declare a variable in Rust.
By default, a variable in Rust is immutable. Meaning this variable is a constant or will not change.
It’s intentionally made immutable by default for safety and easy concurrency.
let x = 5;
let y = "hallo!";
If you try to change one of the variable, it will return
cannot assign twice to immutable variable `x`
To make a mutable variable, use
let mut x = 5;
Now you can assign a new variable
let mut x = 5; x = 10;
What about const?
It also available in Rust, using
const keyword for a constant value.
const MAX_POINTS: u32 = 100_000;
It might be similar to immutable let. What’s the difference then?
- const can be declared in the global scope.
- It represents a constant expression, not a result of something
- The naming convention is UPPER_CASE