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

A variable in Rust is a named binding to a specific memory location, established using the `let` keyword. By default, variable bindings in Rust are strictly immutable, meaning their assigned value or underlying data cannot be modified after initialization. Rust enforces strong, static typing with aggressive type inference, resolving types at compile-time.

## Syntax and Type Inference

Variables are declared using `let`. The compiler infers the type based on the assigned value. If explicit typing is required, it is appended after the variable name using a colon (`:`).

```rust theme={"dark"}
// Implicit type inference (defaults to i32 for integers)
let x = 10;

// Explicit type annotation
let y: f64 = 3.14;
```

## Mutability

To allow reassignment or modification of a variable's data, the binding must be explicitly declared as mutable using the `mut` keyword. This alters the binding's contract with the compiler, permitting in-place memory mutation.

```rust theme={"dark"}
// Immutable binding (default)
let a = 5;
// a = 6; // Compiler error: cannot assign twice to immutable variable

// Mutable binding
let mut b = 5;
b = 6; // Valid reassignment
```

## Shadowing

Rust permits variable shadowing, where a subsequent `let` declaration utilizes the exact same name as an existing variable in the current or parent scope. Shadowing allocates a completely new variable, allowing for type transformations while maintaining immutability. The previous variable is obscured until the current scope terminates.

```rust theme={"dark"}
let z = "hello";

// Shadowing 'z' with a new type (usize) and value
let z = z.len(); 

// Shadowing within an inner scope
{
    let z = z * 2;
} // Inner 'z' is dropped; outer 'z' is accessible again
```

## Scope and Initialization

Variables possess lexical scope, remaining valid from the point of declaration until the end of the enclosing block (`{}`). Rust guarantees memory safety by enforcing strict initialization; a variable must be assigned a value before its memory is read.

```rust theme={"dark"}
let uninitialized_var: i32;

// println!("{}", uninitialized_var); // Compiler error: use of possibly-uninitialized variable

// Deferred initialization is valid as long as it occurs before the first read
uninitialized_var = 100; 
```

## Variables vs. Constants

Variables (`let`) differ fundamentally from constants (`const`). Constants are evaluated at compile-time, are perpetually immutable (cannot use `mut`), require explicit type annotations, and can be declared in the global scope.

```rust theme={"dark"}
// Constant declaration
const MAX_CAPACITY: u32 = 100_000;
```

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