Summary
-
Variables in Rust are, by default, immutable. Immutable variables can be shadowed, but never changed.
-
You can make a variable mutable with the
mut
keyword in itslet
statement. -
Once mutable, you can change the value of a variable with assignment (e.g.,
x = 5;
) and mutation operators (e.g.,x += 2;
). -
Mutation is almost always necessary to perform iteration.
-
A
while
expression is like anif
expression, but continues to evaluate the body as long as the condition remains true. -
You can nest loops inside each other to create more powerful loops.
-
You can use
let
to declare variables without assigning, though you probably don’t want to.
Get hands-on with 1400+ tech skills courses.