In Rust, variables are declared using let. By default, all variables are immutable, which means you cannot change their values. To make a variable mutable, add mut like this: let mut x = 5. This is different from JavaScript, where variables are often mutable by default.
Rust Learning Path
- Introduction to Rust for JavaScript Developers
- Setting Up Rust Development Environment
- Your First Rust Program
- Variables and Mutability in Rust
- Constants and Immutability in Rust
- Working with Strings in Rust
When you begin writing real programs in Rust, you need to store and update values. Rust handles variables in a way that is strict but helpful. In this post, I will explain how variable binding works in Rust and why the language prefers immutability by default.
If you have not created a project yet, check out my earlier posts on Setting Up the Rust Environment and Hello World in Rust to get started.
Declaring a Variable in Rust
The basic syntax for declaring a variable is:
let x = 10;
This line creates a variable named x with the value 10. In Rust, once you assign a value to x, you cannot change it. This is called immutability.
If you try to do this:
x = 20;
The compiler will show an error. It says the variable is not mutable.
Making a Variable Mutable
If you want to change the value of a variable, you must mark it as mutable:
let mut x = 10;
x = 20;
Now Rust knows that x is allowed to change. The mut keyword tells the compiler that you plan to modify this value later.
Why Rust Chooses Immutable by Default
At first, it may feel strange that variables are immutable unless you say otherwise. In JavaScript, you can use let or var to declare variables, and they are usually mutable. But Rust makes immutability the default to help you avoid unexpected changes in your code.
This helps with safety and predictability. If a variable cannot change, you never have to worry about its value being modified by mistake.
Example Program
Here is a short program that shows both immutable and mutable variables:
fn main() {
let name = "Alice"; // immutable
let mut score = 50; // mutable
println!("Name: {}", name);
println!("Score: {}", score);
score = 75;
println!("Updated Score: {}", score);
}
Try running this in a project. You will see that changing the value of score works because it is marked as mutable. But if you try to change name, the compiler will stop you.
Shadowing a Variable
Rust also lets you shadow a variable. This means you can declare a new variable with the same name:
fn main() {
let count = 5;
let count = count + 1;
println!("Count is: {}", count);
}
In this example, you are not mutating the variable. You are creating a new one that shadows the old one. Shadowing is useful when you want to transform a value without changing mutability.
For example, if you get a string and want to trim it:
let input = " hello ";
let input = input.trim();
println!("Cleaned input: {}", input);
This keeps the original variable name while giving it a new value.
Summary
Rust treats all variables as immutable by default. To make them changeable, use mut. This design helps catch bugs early and encourages safer code. Shadowing lets you reuse variable names without mutability. Constants are for values that never change.
In the next post, we will look at Constants and Immutability in more detail. You will learn how to define values that do not change and when to use const vs regular variables.
Next up: Constants and Immutability in Rust