Rust Variables

Rust Tutorial

Rust Variables – Complete Beginner Guide

Variables are one of the most fundamental concepts in Rust programming. If you understand how variables work in Rust, you’ll build a strong foundation for mastering ownership, borrowing, memory safety, and performance.

Rust variables are powerful because they are:

  • Immutable by default

  • Strongly typed

  • Memory-safe

  • Ownership-aware

In this fully beginner guide, you’ll learn:

  • What variables are in Rust

  • How to declare variables

  • Mutability

  • Type annotations

  • Shadowing

  • Constants

  • Variable scope

  • Best practices

  • Common beginner mistakes

Let’s get started


What Are Variables in Rust?

A variable is a name that stores a value.

In Rust, variables are declared using the let keyword.

Basic example:


 

Here:

  • let declares the variable

  • x is the variable name

  • 10 is the value

Rust automatically infers the type.


Immutable by Default (Very Important)

One of Rust’s unique features is:

Variables are immutable by default.

Example:


 

Rust does this for safety and predictability.


Making Variables Mutable

If you want to change a variable, use mut.


 

Now x can be modified.


Why Rust Prefers Immutability

Immutability:

  • Prevents accidental changes

  • Reduces bugs

  • Makes code easier to understand

  • Helps with concurrency safety

Rust encourages safe programming habits.


Type Inference in Rust

Rust is statically typed, but it can infer types automatically.


 

Rust understands this is an i32 by default.


Explicit Type Annotation

You can specify type manually:


 

Syntax:


 


Common Rust Variable Types

TypeExample
i32let x: i32 = 5;
f64let pi: f64 = 3.14;
boollet is_ready: bool = true;
charlet letter: char = 'A';
&strlet name = "Rust";
Stringlet s = String::from("Rust");

Variables and Memory

Rust handles memory automatically.

When a variable goes out of scope, it is dropped.

Example:


 

Rust frees memory automatically.


Shadowing in Rust

Rust allows variable shadowing.


 

Each let x creates a new variable.


Shadowing vs Mutability

Mutability changes value:


 

Shadowing creates new variable:


 

Shadowing allows changing type.

Example:


 

Type changes from &str to usize.


Constants in Rust

Constants are declared using const.


 

Rules:

  • Must declare type

  • Cannot use mut

  • Value must be constant expression

  • Naming convention: UPPERCASE


Difference Between let and const

Featureletconst
MutableYes (with mut)No
Type requiredNoYes
Evaluated at runtimeYesCompile-time
Scope limitedYesGlobal or local

Destructuring Variables

Rust allows unpacking values.

Example with tuple:


 

Example with struct:


 


Variable Scope in Rust

Scope defines where variable is accessible.


 

Variables exist only inside their block.


Using Variables in Functions


 

Parameters are also variables.

They follow ownership rules.


Variables and Ownership (Preview Concept)

For simple types:


 

For String:


 

Understanding variables is crucial for ownership.


Visual Overview of Rust Variables

Rust Variables

Best Practices for Rust Variables

  •  Prefer immutability
  •  Use meaningful names
  •  Use shadowing wisely
  •  Avoid unnecessary mut
  •  Keep scope small

Common Beginner Mistakes

  •  Forgetting mut
  •  Confusing shadowing with mutability
  •  Trying to change immutable variable
  •  Not understanding scope
  •  Ignoring type mismatches

Rust compiler gives helpful error messages.


Example – Real-World Variable Usage


 

Clean, readable, safe.


Frequently Asked Questions (FAQs)

1. Are variables mutable by default in Rust?

No. Variables are immutable by default.


2. How do I make a variable mutable?

Use the mut keyword.


3. What is shadowing in Rust?

Shadowing means declaring a new variable with the same name.


4. What is the difference between let and const?

let declares variables. const declares compile-time constants.


5. Does Rust require type annotations?

Not always. Rust can infer types automatically.


Conclusion

Rust variables are:

  • Immutable by default

  • Strongly typed

  • Memory-safe

  • Ownership-aware

You learned:

  • Variable declaration

  • Mutability

  • Type annotations

  • Shadowing

  • Constants

  • Scope

Understanding variables prepares you for advanced topics like ownership and borrowing.

You may also like...