> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust Mutable Variable

A mutable variable in Rust is a named memory binding, explicitly declared with the `mut` keyword, that permits the reassignment of its value and the in-place modification of its underlying data after initial initialization. Because Rust enforces immutability by default to guarantee memory safety and concurrency, mutability must be explicitly opted into at the binding level.

## Syntax

The `mut` keyword is placed immediately after the `let` keyword and before the variable identifier.

```rust theme={"dark"}
let mut identifier: Type = value;
```

## Core Mechanics

**1. Strict Type Adherence**
While `mut` allows the value of a variable to change, it does not allow the type to change. Rust is statically typed; the memory layout allocated for the variable is fixed at compile time.

```rust theme={"dark"}
let mut counter = 0; // Inferred as i32
counter = 1;         // Valid: Reassignment of the same type

// counter = "Done"; // Compiler Error: expected integer, found `&str`
```

**2. Compound Data Modification**
When a compound data type (like a `struct`, `enum`, or `tuple`) is bound to a mutable variable, the mutability cascades to all of its fields and elements. Rust does not support field-level mutability modifiers on structs; the entire instance is either mutable or immutable based on its binding.

```rust theme={"dark"}
struct Point {
    x: i32,
    y: i32,
}

let mut p = Point { x: 0, y: 0 };
p.x = 5; // Valid: 'p' is mutable, so its fields can be modified
```

**3. Prerequisite for Mutable Borrowing**
Rust's borrow checker dictates that you cannot take a mutable reference (`&mut T`) to data unless the underlying variable binding is itself declared as mutable.

```rust theme={"dark"}
let mut value = 10;
let value_ref = &mut value; // Valid: 'value' is declared with 'mut'
*value_ref += 5;            // Dereference and modify

let strict_value = 10;
// let strict_ref = &mut strict_value; // Compiler Error: cannot borrow as mutable
```

## Mutability vs. Shadowing

Mutability modifies the data within an existing memory location. This is mechanically distinct from variable shadowing, which uses the `let` keyword again to create an entirely new variable (and a new memory allocation) that happens to share the same identifier.

```rust theme={"dark"}
// Mutability: Reuses the same memory location, type must remain the same
let mut a = 5;
a = 10; 

// Shadowing: Creates a new memory location, type can change
let b = 5;
let b = "text"; 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
