• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds
  • Featured on DanielLaunches

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Introduction to Rust -

Lesson 3 of 22

Lesson Progress: 0%

Lesson Progress: 0%
Lesson Incomplete
← Previous: Comments
Lesson 3 of 22
Next: Mutable Variables →

In Rust, variables are immutable by default — once a value is bound to a name, it cannot be changed. You create a variable with the let keyword, and the compiler guarantees that the value stays the same throughout its lifetime. Immutability is a key safety feature: it prevents accidental modifications and makes code easier to reason about. If you need a new value, you can use shadowing, which creates a fresh variable that hides the previous one. Shadowing lets you reuse a variable name while keeping each binding immutable. This lesson explores how immutable variables work in Rust through simple examples.

Code Example
fn main() {
    let x = 5;
    println!("{}", x);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • let x = 5; creates a new variable named x and binds it to the value 5.
  • Variables in Rust are immutable by default, so x will always hold 5.
  • Rust infers the type of x as i32 (a 32-bit integer) automatically.
  • println!("{}", x); prints the value of x, which is 5.
  • Trying to reassign x = 10; later would cause a compiler error.
Code Example
fn main() {
    let a = 10;
    let b = 20;
    println!("{}", a + b);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can create multiple immutable variables in the same program: a holds 10 and b holds 20.
  • Immutable variables can be used in expressions like a + b to compute new values.
  • The addition a + b produces 30, which is passed to println!.
  • Both a and bkeep their original values — immutability means nothing changes.
  • Using multiple variables together is how you build logic from simple building blocks.
Code Example
fn main() {
    let msg = "Hi";
    println!("{}", msg);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Immutable variables can hold text too: let msg = "Hi"; stores a string literal.
  • Rust infers the type of msg as &str (a string slice) automatically.
  • Because msg is immutable, it will always contain "Hi".
  • Printing msg with println! displays Hi on the screen.
  • Immutability applies to all types — numbers, strings, and everything else behave the same way.
Code Example
fn main() {
    let x = 5;
    let x = x + 1;
    println!("{}", x);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Shadowing lets you declare a new variable with the same name using a second let.
  • The first let x = 5; binds x to 5, then let x = x + 1; creates a new binding.
  • The original x is still 5, but the new x shadows it with the value 6.
  • Despite shadowing, each xis individually immutable — you cannot reassign without let.
  • The program prints 6 because the second binding is the one in scope.
Code Example
fn main() {
    let a = 7;
    let b = 3;
    println!("{}", a - b);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Two immutable variables a and b hold the values 7 and 3.
  • The expression a - b subtracts b from a, producing 4.
  • Neither a nor bis modified by the subtraction — the result is a new temporary value.
  • Immutable variables are safe to use in any expression because their values never change.
  • The output is 4, confirming the arithmetic works as expected.
Code Example
fn main() {
    let x = 1;
    let x = "one";
    println!("{}", x);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Shadowing can even change the type of a variable: the first x is a number, the second is a string.
  • let x = 1; creates an integer, while let x = "one"; creates a &str.
  • The old integer x is replaced by the new string x— they are separate variables.
  • This is different from mutation: the original value is not changed, it is simply discarded.
  • The program prints one because the second shadowing binding is active.

Test Incomplete

What keyword is used to create a variable in Rust?

Question #

1/15

Score

0/0 - 0.0 %