Variables
Variales are names given to computer memory location where your store data in a program. In Rust, we define variables with let
:
|
|
In Rust we have two types of variables which are:
- Immutable variables: Default behaviour, you can’t modify its value.
- Mutable variables: You can modify the value of the variable.
|
|
You have to specify
mut
to make variables mutable.
Constants
Constants are variables that can’t be converted to mutable by design and have an explicit data type.
|
|
Constants must be written in uppercase by convention.
Shadowing
Shadowing is a Rust concept of assigning a new value to an immutable variable. With an example is easier to understand:
|
|
Rust programmers say that the first variable declaration is being shadowed by the second.
Shadowing vs Mutable variables
- Without using
let
, script will fail. - You can only do certain transformations:
- Change data type.
- Change value adressing the previous value.
- …
- After a modification has been made, the variable will remain immutable.