Rust variables


Agenda
In this blog, we’ll explore Rust’s variable and mutability, breaking down how they work and why they matter. This isn’t a long read—if you’re just reading, it’ll take about 5 minutes. But if you’re following along and running the example code, 15 minutes should be enough to finish.
Let’s begin,
In Rust, we declare variables using let
or const
, each serving a distinct purpose in managing values efficiently and safely. Keep in mind that, whenever we declare a const
variable, we must specify its data type, like i32
, i64
, or u32
. But for let
, this is optional—Rust will infer the type automatically but it is recommended to specify a data type . In Rust if you declare a variable but you don’t use it, Rust compiler will show you an warning, that the variable is unused.
let
and const
example:
// In Rust we write every line of inside main function.
fn main(){
let student_name = "John";
const STUDENT_AGE: i32 = 16;
// Rust's naming convention for constants is to use uppercase with underscore between.
}
In Rust, variables are immutable by default. This means that once a variable is assigned a value, it cannot be reassigned to a different value. This enhances the safety and performance optimization and allowing compiler to compile code efficiently. If you try to reassign a variable with a new value, the Rust compiler will throw an error, enforcing immutability by default.
Example: reassign mutable variable
fn main(){
let student_name = "John";
const STUDENT_AGE: i32 = 16;
// reassignin student name
student_name = "Steve"
}
If you run the above code you will see the below error.
“cannot assign twice to immutable variable”
How to compile and run rust code?
rustc
main.rs
, this will compile your your code. Now you have to run ./main
(if you are on Mac/Linux) or ./main.exe
(if you are on Windows)."So what does it mean, we cannot reassign a variable? Well, we actually can-by placing the mut
before the variable name. If you use mut
before a variable name, the compiler understands that you intentionally want to make it mutable.
mut
example:
fn main(){
// variable using mut
let mut student_name = "John";
println!("Student name: {student_name}");
const STUDENT_AGE: i32 = 16;
// reassignin student name
student_name = "Steve";
println!("Student name: {student_name}");
}
If you compile the above program, you’ll see that in successfully compiles, but with a warning about the unused STUDENT_AGE
variable. And if you run the compiled code you will see that both names are printed on the terminal.
On the other hand, const
variables are not only immutable by default, they can never be changed to mutable. You can use const
variable in any part of your code without worrying about unexpected modifications.
Shadowing in Rust
Shadowing is when you declare a new variable with the same name as one you previously declared.
Let’s say that you have declared a variable
name car_name
with the value “Porsche 911”
, now you can declare a new variable
with the same name like: car_name
with the value “Aston Martin DB11”
. This new variable now not only overshadowing the old value but also it can also override the data type of that variable. You can change/mutate a variable’s value using mut
, but you can not change the data type. If you try do so, you will get an error.
// you should run this code block on your machine to get an better idea.
fn main(){
// declaring variables
let car_name = "Porsche 911";
let car_price = "0.5 Mill";
println!("Car name is, {car_name}");
println!("Car price in str, INR {car_price}");
// Shadowing a variables
let car_name = "Ferrari Purosangue SUV";
let car_price = 1; // here we are also overshadowing the data type of the variable.
println!("Car name is, {car_name}");
println!("Car price in num, INR {car_price} Mill");
}
Here we are finishing this post, I would encourage you to read the original rust documentation, if you want to go more deep. The link is here for the chapter Variable and Mutability. Thanks for reading, our next post will be on data types. 👋👋👋.
Subscribe to my newsletter
Read articles from RAJU SARKAR directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
