By default, variables are immutable. Although variables are immutable by default, you can make them mutable by adding mut in front of the variable name.
Note: you should be careful about this while switching from Javascript or Typescript
let x = 1; // immutable
let mut y = 2; // mutable
Like immutable variables, constants are values that are bound to a name and are not allowed to change, but there are a few differences between constants and variables.
mut with constants. Constants aren’t just immutable by default—they’re always immutable. You declare constants using the const keyword instead of the let keyword, and the type of the value must be annotated.const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
The compiler is able to evaluate a limited set of operations at compile time, which lets us choose to write out this value in a way that’s easier to understand and verify, rather than setting this constant to the value 10,800.
You can declare a new variable with the same name as a previous variable.
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.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.Rust has three kinds of loops: loop, while, and for.
The loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.