Chapter 7: Rust Variables

Rust programming: Variables

In Rust, variables are not like in Python or JavaScript where everything changes freely. Rust makes very deliberate choices about variables to keep your code safe, predictable, and fast. This is where the famous “Rust way” starts to shine (and sometimes frustrate beginners for the first week or two 😄).

Let me teach this like we’re sitting together with VS Code open, going line-by-line, with analogies from everyday life in Hyderabad, lots of examples you can cargo run right now, and clear explanations of why Rust does things this way.

1. Declaring Variables — The let Keyword (No var, no int x =)

Rust uses one main keyword for variables: let

Rust
  • No type needed most times → Rust infers it (great productivity!)
  • But you can write types explicitly (good for clarity or when compiler can’t guess):
Rust

2. Immutable by Default — The #1 Most Important Rule

In Rust: Variables are immutable unless you say otherwise.

Rust

Why does Rust force this?

  • Safety: Most bugs come from accidentally changing values you didn’t mean to.
  • Reasoning: When reading code, if no mut, you know this value never changes → easier to understand big programs.
  • Concurrency safety: Later when we do threads, immutable = no data races by default.

Think of it like: Your biryani plate at Paradise → once served, you don’t want someone randomly adding extra mirchi without telling you. Rust prevents that by default.

3. Making Variables Mutable — Add mut

Only when you really need to change it:

Rust
  • mut goes afterlet, before the name
  • You can only mutate variables you own (more on ownership soon)

4. Constants — const (Different from immutable variables)

Constants are always immutable, global-ish, and must have type written.

Rust

Differences from let:

Feature let (immutable) const
Keyword let const
Mutability Can add mut Always immutable
Type annotation Optional (inferred) Required
Scope Block scope Can be file / crate global
Can be computed at runtime? Yes No — compile-time only
Common use Normal variables Magic numbers, config limits

Use const for things like config values, math constants, limits.

5. Shadowing — Re-declare with let (Very Rust-y Feature!)

Shadowing lets you create a new variable with the same name as an old one.

Rust

Why is this useful?

  • Transform data without inventing names like city_temp, city_str, final_city
  • Change type safely (e.g. String → usize → &str)
  • Keep code clean — no forced mut just to reuse a name

Shadowing ≠ mutation → Old value still exists until it goes out of scope, but new let hides it.

6. Quick Ownership Teaser (Variables + Memory)

Variables own their data. When variable goes out of scope → data dropped (freed).

Rust

This is why let + no mut is powerful — compiler tracks who owns what.

(We’ll go deep into ownership next if you want!)

Full Small Example to Try Right Now

Rust

Run → play with changing values, add mut where needed, shadow types.

Summary in Teacher Voice

Rust Variables =

  • Declared with let
  • Immutable by default → huge safety + readability win
  • mut only when you need change
  • const for true constants (compile-time, typed)
  • Shadowing lets you reuse names for transformations (no forced mut)
  • Everything ties into ownership — variables own data, moves transfer ownership

This is the foundation. Once you love immutable-by-default + shadowing, Rust starts feeling elegant.

Ready for next?

  • Deep dive into ownership & borrowing (the real Rust magic)?
  • Data types in detail (integers, floats, chars, bool, tuples, arrays)?
  • Or functions + how variables work inside them?

Just say the word — your personal Rust teacher is ready! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *