Variables, mut and shadowing in Rust

Learn Rust · Beginner · 10 min · After Install Rust and run your first program

Why can't you change a variable in Rust by default?

  1. 1Guess

    fn main() {
        let x = 5;
        let x = x + 1;
        {
            let x = x * 2;
            println!("Inner x is {x}");
        }
        println!("Outer x is {x}");
    }
    
    What does this program print?
  2. 2Read

    Immutable by default

    A variable made with let cannot be assigned to a second time. Add mut to allow it: let mut count = 0. The Book gives the reason: the compiler guarantees a value you did not mark mut never changes, so you do not have to track it. A constant uses const, is always immutable, needs a type, and its name is written in capitals.

  3. 3Code

    mut, const and a typed variable

    1 / 2
    const MAX_COUNT: u32 = 3;
    
    fn main() {
        let mut count = 0;
        let price: f64 = 9.5;
        count += 1;
        count += 1;
        println!("count is {count} of {MAX_COUNT}");
        println!("price is {price}");
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Shadowing is not mut

    Shadowing means declaring a new variable with the same name: let x = x + 1. The old variable is hidden from then on. This differs from mut in one way that matters: a new let makes a new variable, so it may have a different type. With mut you can change the value but never the type.

  5. 5Code

    Shadowing that changes the type

    1 / 2
    fn main() {
        let spaces = "   ";
        let spaces = spaces.len();
        println!("{spaces}");
        let whole = 7 / 2;
        let exact = 7.0 / 2.0;
        println!("{whole} {exact}");
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    fn main() {
        let score = 10;
        println!("Score: {score}");
        score = 20;
        println!("Score: {score}");
    }
    
    This program does not compile. What is the bug?
  7. 7Say it back

  8. 8Mission · 8 min

    Write a program that makes a mut counter, adds to it twice and prints it. Then shadow a text variable with its length and print that. Try removing mut and read the error.

    cargo new vars
    cd vars
    cargo run

Free AI coding tools to practise with

  1. AgentRouter Free Credits

    Free AI coding credits: $25 per daily check-in and $50 per invite

    No cardVerified

  2. Cursor Free Plan

    Cursor Agent with limited agent and chat requests, $0/month

    No cardVerified

  3. Cline Free Models

    Try select models free in Cline via rotating promotions with a usage quota

    No cardVerified

All free ai coding tools →

Common questions

Why are variables immutable by default in Rust?
It is a safety choice. When a value is not marked mut, the compiler guarantees it never changes, so you can read the code without checking whether something else modified it. If you do need to change a value, you add mut to the declaration and the intent is visible to every reader.
What is shadowing in Rust?
Shadowing is declaring a new variable with the same name as an earlier one, for example let x = x + 1. The new variable hides the old one until its scope ends. Because it is a new variable, it can have a different type, which a mut variable cannot. Shadowing inside a block ends when the block ends.
What is the difference between const and let in Rust?
A let variable is immutable unless you add mut, and its type can be inferred. A const is always immutable, cannot use mut, must have a type written out and may only be set to a constant expression. Constants can be declared in any scope, including outside every function, and are named in capitals.

Sources

Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer