HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RustVariablesData TypesOperatorsStringsVectors (Vec)Conditions (if / match)Loops (for / while)FunctionsOwnership & Borrowing
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Vector & HashMap
🔁 Condition & Loop
🧩 Function
🔑 Ownership & Borrowing
Rust›Variables

Variables

let (immutable), let mut (mutable), const, and Rust's safety philosophy.

Simple enough for a 5-year-old to understand

let and let mut

Variables in Rust are IMMUTABLE by default. You need let mut to change them.

Example code
1
fn main() {
2
let x = 5;
Constant, cannot be changed.
VariableConstant, cannot be changed.
3
let mut y = 10;
Mutable (mut).
VariableMutable (mut).
4
y = 20;
It can change because it is mut.
VariableIt can change because it is mut.
5
println!("{} {}", x, y);
5 20
Output / Input5 20
6
}
VariableOutput / Input
💡 Hint: Modifying without mut causes a compilation error — this is Rust's safety rule.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Define and print let city = "Edirne".

🎯 Expected output: Edirne

Loading editor…

About this topic

Variables in Rust are IMMUTABLE by default. Assigning a value a second time to a variable defined with let is a compilation error. If you want to change it, you must explicitly declare this with the mut keyword.

This is the opposite default of other languages and is intentional. When immutability is the default, you only need to look at the code to see if a value changes; if mut is not written, that value never changes. In concurrent programming, this guarantee is very valuable.

Rust's second unusual feature is shadowing: you can define a new variable with the same name. This is not an assignment, it is a new variable and its type can even be different. It is frequently used to convert text to a number while keeping the same name.

What you will learn

  • defining variables with let
  • that immutability is the default and what mut does
  • the difference between shadowing and assignment
  • defining constants with const

When to use?

Do not write mut if the value will not change. The compiler warns about unused mut, and paying attention to this warning makes the intent of the code clear.

Shadowing is used when you want to transform a value and continue with the same name; converting text to a number is the classic example of this.

Common mistakes

Wrong

fn main() {
    let count = 5;
    count = 10;
    println!("{}", count);
}

A variable defined with let cannot be changed; the compiler gives a "cannot assign twice to immutable variable count" error and suggests adding mut. This is Rust's most surprising default, but it is a conscious design choice.

Correct

fn main() {
    let mut count = 5;
    count = 10;
    println!("{}", count);
}

Wrong

fn main() {
    let total;
    println!("{}", total);
}

Rust does NOT allow you to read an uninitialized variable; the compiler gives a "used binding total isn't initialized" error. C and C++ read the garbage value in memory in this case and silently run incorrectly; Rust does not compile the code at all.

Correct

fn main() {
    let total = 0;
    println!("{}", total);
}

Wrong

fn main() {
    const LIMIT = 100;
    println!("{}", LIMIT);
}

It is MANDATORY to specify the type in a const definition; the compiler gives a "missing type for const item" error. Type inference works with let, but because const is evaluated at compile time and can be used anywhere, the type must be explicit.

Correct

fn main() {
    const LIMIT: i32 = 100;
    println!("{}", LIMIT);
}

Frequently asked questions

What is the difference between shadowing and mut?

mut modifies the same variable; shadowing creates a new variable. The difference practically shows up in the type: you cannot change the type with mut, you can change it with shadowing. Also, the shadowed variable becomes inaccessible after the new definition.

Can't I just make everything mut?

It compiles, but you lose the guarantee provided by Rust. The compiler warns for unused mut. More importantly, immutable values can be safely shared between threads; mutable ones cannot, and in concurrent code, this distinction is decisive.

What is the difference between const and an immutable let?

const is calculated at compile time, specifying the type is mandatory, and it can be defined in any scope. An immutable let, on the other hand, can be bound to a value calculated at runtime. const is used for true constants, let is used for calculated values.

Summary

Variables are immutable by default; mut is required to change them.

Reading an uninitialized variable is a compilation error.

Shadowing creates a new variable and allows changing the type.

It is mandatory to write the type in a const definition.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code