> ## 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 Shadowing

Shadowing in Rust occurs when a new variable is declared with the exact same identifier as a previously declared variable within the same or an enclosing scope. The compiler resolves any subsequent references to that identifier to the most recently declared variable, effectively obscuring the original variable from that point forward.

To shadow a variable, the `let` keyword must be used for the new declaration. Because shadowing creates a completely new variable binding rather than modifying an existing one, the original variable does not need to be declared with the `mut` keyword. Furthermore, because a new variable is instantiated, the data type associated with the identifier can be changed.

```rust theme={"dark"}
let x = 5;       // Original binding (type: i32)
let x = x + 1;   // Shadows the first 'x' (type: i32)
let x = "six";   // Shadows the second 'x', changing the type (type: &str)
```

Shadowing is lexically scoped. When a variable is shadowed within an inner block, the shadowing effect is strictly bound to that block. Once the inner scope terminates, the inner variable is dropped, and the identifier reverts to resolving to the original variable in the outer scope.

```rust theme={"dark"}
let y = 10;

{
    // Shadows the outer 'y' strictly within this block
    let y = y * 2; 
    println!("Inner y: {}", y); // Outputs: 20
} // The inner 'y' goes out of scope and is dropped

// The identifier 'y' resolves to the outer variable again
println!("Outer y: {}", y); // Outputs: 10
```

It is critical to distinguish shadowing from variable mutation (reassignment). They are distinct mechanical operations in the Rust compiler:

* **Shadowing** requires the `let` keyword, creates a new variable binding, allows the data type to change, and results in a variable that is immutable by default.
* **Mutation** omits the `let` keyword, requires the original variable to be declared as `mut`, modifies the value in the existing memory location, and strictly enforces that the new value matches the original data type.

```rust theme={"dark"}
// Shadowing: Valid type change
let spaces = "   ";
let spaces = spaces.len(); // Rebinds 'spaces' to a usize

// Mutation: Invalid type change (Compile-time Error)
let mut spaces_mut = "   ";
spaces_mut = spaces_mut.len(); // ERROR: expected &str, found usize
```

<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>
