View : 109

22/04/2026 07:11am

Rust programming code snippet showing variable declaration

Variables & Mutability: Why Immutable by Default? | Rust The Series EP.4

#Rust Programming

#Rust

#Variables

#Mutability

#Immutable by default

#Rust tutorial

#Software Development

After getting to know Cargo and creating our first project in EP.3, it’s finally time to start writing actual code! The most fundamental starting point for any programming language is "Variables."

However, in Rust, variables are handled with a level of "strictness" that might surprise you if you're coming from other languages. Let’s dive in.

1. Immutable by Default: The Safety First Approach

In most programming languages, once you declare a variable, you can change its value whenever you like. But in Rust, "all variables are immutable (unchangeable) by default."

Take a look at this code:

Rust

fn main() {
    let x = 5;
    println!("The value of x is: {x}");
    
    x = 6; // ❌ This line will cause a Compiler Error!
    println!("The value of x is: {x}");
}

Why did Rust designers choose this?

The primary reasons are Safety and Concurrency:

  • Fewer Bugs: If you assume a value stays the same but it changes elsewhere in your program, it leads to hard-to-track bugs. Rust prevents this at compile time.

  • Readability: When you see let x = ..., you can be certain that x will remain constant throughout its scope.

  • Performance: The compiler can optimize the code more effectively when it knows a value will not change.

2. Using mut to Allow Changes

Of course, in real-world applications, we often need variables that can change (such as loop counters or application states). Rust provides the mut keyword (short for mutable) for this purpose.

Rust

fn main() {
    let mut x = 5; // ✅ Adding 'mut' makes it mutable
    println!("The value of x is: {x}");
    
    x = 6; // ✅ Now we can change the value!
    println!("The value of x is: {x}");
}

Adding mut is a clear signal to both the compiler and your teammates: "This variable is intended to be changed." This clarity significantly improves code quality.

3. Constants vs. Immutable Variables

You might be wondering, "If immutable variables can't change, how are they different from Constants?"

Feature

Variables (Immutable)

Constants (const)

Use of mut

Can use mut to become mutable

Never allowed to be mutable

Declaration

Uses let

Uses const and must include a Type

Value Source

Can be determined at Runtime

Must be a constant known at Compile-time

Example of a Constant:

Rust

const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;

4. Shadowing: Re-declaring the Same Name

One of Rust's coolest features is Shadowing. This allows you to declare a new variable using the same name as a previous one.

Rust

fn main() {
    let x = 5;
    let x = x + 1; // Shadowing (First time)
    
    {
        let x = x * 2; // Shadowing within this specific scope
        println!("The value of x in the inner scope is: {x}"); // Result: 12
    }

    println!("The value of x is: {x}"); // Result: 6
}

How is Shadowing different from mut?

  1. New Identity: Shadowing creates a new variable (the old one is hidden). mut simply updates the value of the existing variable.

  2. Type Flexibility: With shadowing, we can change the Data Type while keeping the same name (e.g., transforming a String into a Number).


Summary

In Rust, choosing between Immutable, mut, or Shadowing helps you write code that is safe, predictable, and highly efficient in memory management.

In EP.5, we will explore the perfect companion to variables: Basic Data Types. We’ll take a deep dive into how Rust handles numbers, characters, and groups of data (Scalar and Compound types). Don't miss it!

Happy Coding with Rust

🎯 Stay Tuned with Superdev Academy:

Don't miss out on deep-dive technical articles and updates. Follow us on all platforms: