Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Variables and Mutability | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 5 of 50

·rust

Variables and Mutability

12 minutes

Course progress10%

Variables and Mutability

One of Rust's core principles is immutability by default. This prevents entire categories of bugs and encourages careful thinking about what data your program modifies.

Immutable Variables

All variables are immutable by default:

fn main() {
    let x = 5;
    (\
println!
"x = {}\", x); // prints 5 // x = 6; // ERROR! }

Trying to modify an immutable variable produces a compiler error. This is a feature, not a limitation—the compiler prevents accidental mutations.

Mutable Variables

Explicitly mark variables as mutable with mut:

fn main() {
    let mut x = 5;
    println!(\"x = {}\", x);  // prints 5
    x = 6;
    println!(\"x = {}\", x);  // prints 6
}

The mut keyword signals intent: "This variable will change." This self-documents code.

Why Immutability by Default?

Safety

Immutable variables can't change unexpectedly.

Concurrency

Immutable data is thread-safe. Multiple threads safely read immutable values without synchronization.

Reasoning About Code

Immediate clarity about whether a value can change.

Variable Shadowing

Declare a new variable with the same name, creating a new binding:

fn main() {
    let x = 5;
    let x = x + 1;  // Shadow: new binding
    let x = x * 2;  // Another shadow
    println!(\"x = {}\", x);  // prints 12
}

Shadowing differs from mutability:

  • Shadowing: Creates new variable (possibly different type)
  • Mutability: Changes value of same variable

Use shadowing for transformations:

fn main() {
    let input = \"  42  \";
    let input = input.trim();        // Shadow: trimmed string
    let input: i32 = input.parse().unwrap();  // Shadow: parse to number
    println!(\"Number: {}\", input + 10);  // 52
}

Constants vs Immutable Variables

const MAX_POINTS: u32 = 100_000;   // Constant
let fixed_value = 42;               // Immutable variable
Aspectconstlet
EvaluatedCompile timeRuntime
TypeRequiredInferred
ScopeGlobalBlock

Use constants for compile-time known values.

Type Annotations

Explicitly specify types, though Rust usually infers them:

let x = 5;              // Inferred as i32
let x: i32 = 5;        // Explicit
let mut y: f64 = 3.14; // Explicit on mutable

Practical Example

fn main() {
    let username = \"alice\";  // Immutable
    println!(\"User: {}\", username);
    
    let mut score = 0;  // Mutable for game tracking
    score += 10;
    score += 5;
    println!(\"Score: {}\", score);
    
    const WINNING_SCORE: i32 = 100;
    if score >= WINNING_SCORE {
        println!(\"Victory!\");
    }
}

Best Practices

  1. Start immutable: Add mut only when necessary
  2. Shadow for transformations: When data changes type or meaning
  3. Constants for config: Fixed values used throughout code
  4. Clear intent: mut shows your intentions to other developers
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonUnderstanding Cargo ProjectsContinue to lesson 6Basic Data TypesBack to Rust Fundamentals curriculum