Mutability
By default variables in Rust are immutable. To make a variable mutable one must explicity add `mut` in front of it
Constants
Constatns are values that are bound to a name and are not allowed to change. Some differences with variables:
- You can’t use `mut` with constants
- Constants are delared with `const` keyword instead of `let`
- Type value must be annotated
- Constants can be declared in any scope
- Constants may only be set to a constant expression, not the result of a function call of any other value that could only be computed at runtime
Shadowing
You can declare a new variable with the same value as a previous variable. The new variable “shadows” the previous variable
Shadowing is different from marking a variable as mut, because we’ll get a compile-time error if we accidentally try to reassign to this variable without using the let keyword. By using let, we can perform a few transformations on a value but have the variable be immutable after those transformations have been completed.
The other difference between mut and shadowing is that because we’re effectively creating a new variable when we use the let keyword again, we can change the type of the value but reuse the same name. For example, say our program asks a user to show how many spaces they want between some text by inputting space characters, but we really want to store that input as a number:
Data types
Tuple
Tuple elements can be accessed directly by using a period.
Array
In Rust every element of an array must have the same type. They also have a fixed length, like tuples.
You would write an array’s type by using square brackets, and within the brackets include the type of each element, a semicolon, and then the number of elements in the array, like so:
You can also easily initialize an array that contains the same value for each element:
Accessing array elements is also straightforward:
Destructuring
Rust also supports destructuring