Bird
Raised Fist0
Rustprogramming~20 mins

Mutable variables in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Mutable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code with mutable variables?

Consider the following Rust code snippet. What will it print?

Rust
fn main() {
    let mut x = 5;
    x = x + 3;
    println!("{}", x);
}
ACompilation error
B5
C8
D0
Attempts:
2 left
๐Ÿ’ก Hint

Remember that mut allows the variable to be changed.

โ“ Predict Output
intermediate
2:00remaining
What error does this Rust code produce?

What error will this Rust code produce?

Rust
fn main() {
    let x = 10;
    x = 20;
    println!("{}", x);
}
APrints 10
BPrints 20
CRuntime error: variable not mutable
DCompilation error: cannot assign twice to immutable variable `x`
Attempts:
2 left
๐Ÿ’ก Hint

Variables are immutable by default in Rust.

๐Ÿ”ง Debug
advanced
2:30remaining
Why does this Rust code fail to compile?

Examine the code below. Why does it fail to compile?

Rust
fn main() {
    let mut x = 1;
    let y = &x;
    x = 2;
    println!("{}", y);
}
ACannot assign to `x` because it is borrowed as immutable by `y`
BPrints 2
CPrints 1
DCompilation error: `y` is not initialized
Attempts:
2 left
๐Ÿ’ก Hint

Rust enforces borrowing rules to prevent data races.

๐Ÿง  Conceptual
advanced
2:00remaining
Which statement about mutable variables in Rust is true?

Choose the correct statement about mutable variables in Rust.

AVariables are immutable by default and must be declared with <code>mut</code> to be changed.
BAll variables are mutable by default unless declared with <code>const</code>.
CMutable variables can be changed even when borrowed immutably.
DDeclaring a variable with <code>let mut</code> disables Rust's ownership rules.
Attempts:
2 left
๐Ÿ’ก Hint

Think about Rust's default variable behavior.

โ“ Predict Output
expert
3:00remaining
What is the output of this Rust code involving mutable references?

Analyze the code and select the output it produces.

Rust
fn main() {
    let mut x = 10;
    {
        let y = &mut x;
        *y += 5;
    }
    println!("{}", x);
}
A10
B15
CCompilation error: cannot borrow `x` as mutable more than once
DRuntime error: mutable borrow out of scope
Attempts:
2 left
๐Ÿ’ก Hint

Mutable references allow changing the value they point to.

Practice

(1/5)
1. What keyword do you use in Rust to make a variable mutable?
easy
A. let
B. var
C. change
D. mut

Solution

  1. Step 1: Understand variable declaration in Rust

    Rust uses let to declare variables, but by default they are immutable.
  2. Step 2: Identify how to make variables mutable

    Adding mut after let makes the variable mutable.
  3. Final Answer:

    mut -> Option D
  4. Quick Check:

    Use let mut for mutable variables [OK]
Hint: Remember: let mut makes variables changeable [OK]
Common Mistakes:
  • Using 'var' instead of 'mut'
  • Forgetting 'mut' and expecting change
  • Using 'let' alone for mutable variables
2. Which of the following is the correct syntax to declare a mutable integer variable named x with value 5 in Rust?
easy
A. let mut x = 5;
B. var mut x = 5;
C. mut let x = 5;
D. let x = 5;

Solution

  1. Step 1: Recall correct order of keywords

    In Rust, let comes first, then mut to mark mutability.
  2. Step 2: Check each option's syntax

    let mut x = 5; uses let mut x = 5; which is correct. Others have wrong order or invalid keywords.
  3. Final Answer:

    let mut x = 5; -> Option A
  4. Quick Check:

    Correct syntax is let mut variable = value; [OK]
Hint: Always write let mut in that order [OK]
Common Mistakes:
  • Swapping 'mut' and 'let'
  • Using 'var' instead of 'let'
  • Omitting 'mut' when variable changes
3. What will be the output of this Rust code?
fn main() {
    let mut count = 1;
    count = count + 2;
    println!("{}", count);
}
medium
A. 3
B. 2
C. 1
D. Compilation error

Solution

  1. Step 1: Understand variable initialization and mutation

    The variable count starts at 1 and is mutable.
  2. Step 2: Calculate the new value after addition

    Adding 2 to 1 gives 3, so count becomes 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Mutable variable updated correctly outputs 3 [OK]
Hint: Mutable lets you add and update variable values [OK]
Common Mistakes:
  • Expecting original value without mutation
  • Thinking code causes error without 'mut'
  • Confusing output with variable name
4. Identify the error in this Rust code snippet:
fn main() {
    let x = 10;
    x = 20;
    println!("{}", x);
}
medium
A. Missing semicolon after println!
B. Variable x is not declared
C. Cannot assign twice to immutable variable x
D. Incorrect println! syntax

Solution

  1. Step 1: Check variable mutability

    Variable x is declared without mut, so it is immutable.
  2. Step 2: Analyze reassignment attempt

    Assigning x = 20; tries to change an immutable variable, causing a compile error.
  3. Final Answer:

    Cannot assign twice to immutable variable x -> Option C
  4. Quick Check:

    Immutable variables cannot be reassigned [OK]
Hint: Add mut to allow variable reassignment [OK]
Common Mistakes:
  • Ignoring need for 'mut' to reassign
  • Thinking println! needs semicolon error
  • Assuming variable is undeclared
5. You want to create a counter that increases by 1 every time a function is called. Which Rust code correctly uses a mutable variable to achieve this?
fn main() {
    let mut counter = 0;
    increment(&mut counter);
    increment(&mut counter);
    println!("{}", counter);
}

fn increment(count: &mut i32) {
    *count += 1;
}
hard
A. Code fails because counter is immutable
B. Code compiles and prints 2
C. Code prints 0 because counter is not updated
D. Code fails due to wrong function signature

Solution

  1. Step 1: Check mutability of counter

    Counter is declared mutable with let mut counter = 0;.
  2. Step 2: Understand function parameter and mutation

    Function increment takes a mutable reference and increases the value by 1 each call.
  3. Step 3: Count increments and final output

    Called twice, counter increases from 0 to 2, then printed.
  4. Final Answer:

    Code compiles and prints 2 -> Option B
  5. Quick Check:

    Mutable variable with mutable reference updates correctly [OK]
Hint: Use let mut and mutable references to update variables [OK]
Common Mistakes:
  • Forgetting 'mut' on variable or reference
  • Passing immutable reference to function
  • Expecting counter to update without mutable reference
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard