Day 2 40 min intermediate

Variables and Mutability

Learn how to declare variables, why they are immutable by default, and how to use constants

Learning Objectives

  • Declare variables with 'let'
  • Understand the difference between immutable and mutable variables
  • Learn about shadowing
  • Use constants

Immutable by Default

In most languages, you can change a variable’s value after creating it. In Rust, variables are immutable by default. Once you bind a value to a name, you can’t change it.

main.rs
1
2
3
4
5
fn main() {
    let x = 5;
    println!("The value of x is: {}", x);
    // x = 6; // ERROR! Cannot assign twice to immutable variable
}

Why Immutable?

Immutability leads to safer, more predictable code. If you know a value won’t change, you don’t have to track how and where it might be modified across your program.

Making Variables Mutable

To make a variable changeable, add the mut keyword:

main.rs
1
2
3
4
5
6
fn main() {
    let mut x = 5;
    println!("The value of x is: {}", x);
    x = 6;
    println!("The value of x is now: {}", x);
}

Constants

Constants are similar to immutable variables, but with key differences:

  1. You use const instead of let.
  2. You must annotate the type.
  3. They can be declared in any scope, including the global scope.
  4. They must be set to a constant expression (something the compiler can calculate at build time).
main.rs
1
2
3
4
5
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;

fn main() {
    println!("Constant value: {}", THREE_HOURS_IN_SECONDS);
}

Shadowing

In Rust, you can declare a new variable with the same name as a previous variable. This is called shadowing. The second variable “shadows” the first, taking its place.

main.rs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fn main() {
    let x = 5;
    let x = x + 1; // x is now 6
    
    {
        let x = x * 2; // x is now 12 (only in this scope)
        println!("The value of x in the inner scope is: {}", x);
    }

    println!("The value of x in the outer scope is: {}", x); // still 6
}

Shadowing vs. mut

  • Shadowing: We are creating a new variable. We can even change the data type of the variable while keeping the same name.
  • mut: We are changing the value of the existing variable. We cannot change the type.
1
2
3
4
5
let spaces = "   ";
let spaces = spaces.len(); // Valid shadowing (String -> usize)

let mut spaces = "   ";
// spaces = spaces.len(); // ERROR! Cannot change type of mutable variable

Summary

Today you learned:

  • Variables are immutable by default for safety.
  • Use mut to allow modification.
  • Constants (const) are for values that never change and require type annotation.
  • Shadowing allows you to reuse variable names with new values or types.

Practice Exercise

  1. Create a variable for your age, make it mutable, and increment it by 1.
  2. Define a constant for the speed of light.
  3. Use shadowing to convert a string "42" into an integer 42.

Next Steps

Tomorrow, we’ll explore Data Types—and see how Rust handles numbers, booleans, and compound structures!

Quiz

Complete this quiz with a minimum score of 80% to mark Day 2 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!