Overview - Mutable variables
What is it?
Mutable variables in Rust are variables that you can change after you create them. By default, variables in Rust cannot be changed once set. To make a variable mutable, you add the keyword 'mut' before its name. This lets you update the value stored in that variable later in your program.
Why it matters
Mutable variables exist because programs often need to change data as they run, like updating a score in a game or changing a setting. Without mutable variables, you would have to create new variables every time you want to change something, which is inefficient and confusing. Rust’s approach helps keep programs safe by making you explicitly say when a variable can change, reducing bugs.
Where it fits
Before learning mutable variables, you should understand how to declare and use basic variables in Rust. After this, you can learn about ownership and borrowing, which explain how Rust manages memory safely when variables change.