DaleSchool

Variables and Values

Beginner15min

Learning Objectives

  • Declare a variable with let
  • Create a mutable variable with the mut keyword
  • Understand that reassigning an immutable variable causes a compile error
  • Print variable values with println!

Working Code

Try running this code in the Rust Playground!

fn main() {
    let name = "DaleSchool";
    let year = 2026;

    println!("Site name: {name}");
    println!("This year is {year}");
}

You can create variables with let and print their values inside println! using the {variable_name} syntax.

Now let's look at changing a value.

fn main() {
    let mut score = 0;
    println!("Initial score: {score}");

    score = 10;
    println!("Updated score: {score}");
}

Adding mut before the variable name lets you change its value later.

Try It Yourself

Try running the code below. You'll get an error!

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

    x = 6; // Error here!
    println!("x = {x}");
}

Can you guess why the error occurs? Then try adding mut next to let to fix it.

"Why?" — Why Prevent Changing Values?

In Rust, variables are immutable by default. Once you assign a value, you can't change it.

You might wonder, "Why is that a good thing?" As code grows longer, it becomes harder to track where a value was changed. Rust keeps values unchangeable by default for safety. When you do want to change a value, just add mut.

There are two ways to print variables with println!.

fn main() {
    let age = 20;

    // Method 1: Write the variable name directly inside braces
    println!("Age: {age}");

    // Method 2: Leave the braces empty and pass the variable after
    println!("Age: {}", age);
}

Both produce the same result. Use whichever feels more comfortable!

Deep Dive

Shadowing — Creating a new variable with the same name

In Rust, you can redeclare a variable with the same name using let. This is called shadowing.

fn main() {
    let x = 5;
    let x = x + 1;
    let x = x * 2;

    println!("x = {x}"); // 12
}

Unlike mut, shadowing creates a brand-new variable each time. It lets you "change" a value without using mut. We'll explore the differences in more detail later.

Running the code below will trigger a compiler error.

fn main() {
    let x = 5;
    x = 6;
}

Error message:

error[E0384]: cannot assign twice to immutable variable `x`
 --> src/main.rs:3:5
  |
2 |     let x = 5;
  |         - first assignment to `x`
3 |     x = 6;
  |     ^^^^^ cannot assign twice to immutable variable

Translation: "You can't assign a value twice to an immutable variable x." Change it to let mut x = 5; and the error goes away.

The compiler caught your mistake early! Read error messages carefully — they usually show you how to fix the problem.

  1. Declare a temperature variable with mut, set it to 20 first, then change it to 25, and print both values.
  2. Store your favorite food's name in a variable and print "My favorite food is {variable}!" using println!.

Q1. What is the output of this code?

fn main() {
    let mut count = 3;
    count = 7;
    println!("{count}");
}
  • A) 3
  • B) 7
  • C) Compile error
  • D) 37

Q2. What keyword do you need to use when declaring a variable whose value you want to change later?

  • A) var
  • B) let
  • C) mut
  • D) change

Q3. Will the following code run successfully?

fn main() {
    let city = "Seoul";
    let city = "Busan";
    println!("{city}");
}
  • A) It causes an error
  • B) "Seoul" is printed
  • C) "Busan" is printed
  • D) Both are printed