Variables & Mutability Questions with Answers

9 practice questions on Variables & Mutability (Rust) with answers and step-by-step solutions. Try 9 samples below, then practise all 9 free in the AptiNest app.

Sample questions

Question 1Easy

Which declaration makes x mutable so it can be reassigned later?

  1. let mut x = 5;
  2. let x mut = 5;
  3. mut let x = 5;
  4. let x: mut = 5;

Answer: A. let mut x = 5;

Explanation

Variables are immutable by default in Rust. mut goes between let and the name, so let mut x = 5; lets you write x = 6 later.

Step-by-step solution

  1. Rust variables are immutable unless you add mut.
  2. The mut keyword goes after let and before the name.
  3. With let mut x = 5;, the later line x = 6; compiles.

Why the other options are wrong

  • B. mut must come right after let, not after the name.
  • C. mut does not go before let.
  • D. mut is not a type annotation; it sits before the name.

Tip: Read it as: let, then mut, then the name.

Question 2Easy

What is the difference between const and an immutable let?

  1. const can be changed later with a plain assignment
  2. let is always global while const is local
  3. const is compile-time and needs a type
  4. They are identical in every respect

Answer: C. const is compile-time and needs a type

Explanation

A const is inlined at compile time and must have an explicit type; a let is a run-time local binding.

Step-by-step solution

  1. const needs a type.
  2. It is compile-time.
  3. It can live at module level.

Why the other options are wrong

  • A. const never changes.
  • B. let is local to its scope.
  • D. They differ in several ways.

Question 3Easy

What happens with let x = 5; x = 6;?

  1. x becomes 6 because assignment always works
  2. x stays 5 and the assignment is silently ignored
  3. It panics at run time when the assignment executes
  4. Compile error: cannot assign twice to immutable variable

Answer: D. Compile error: cannot assign twice to immutable variable

Explanation

Bindings are immutable unless declared let mut. Assigning to one is a compile-time error.

Step-by-step solution

  1. let makes immutable bindings.
  2. Assignment needs mut.
  3. Compiler rejects.

Why the other options are wrong

  • A. Bindings are immutable by default.
  • B. The compiler flags it instead.
  • C. This is caught at compile time.

Question 4Medium

What is printed?

let x = 5;
let y = {
    let x = x * 10;
    x + 1
};
println!("{} {}", x, y);
  1. 50 51
  2. 5 6
  3. 5 51
  4. 51 51

Answer: C. 5 51

Explanation

The block's x is a new binding equal to 50, so the block's value is 51 and y is 51. The outer x is still 5, because the inner x goes out of scope at the closing brace.

Step-by-step solution

  1. The outer x is 5.
  2. Inside the block, a new x = 5 * 10 = 50 shadows it.
  3. The block returns 50 + 1 = 51 for y, and the outer x is still 5.

Why the other options are wrong

  • A. The outer x is still 5 when printed, not 50.
  • B. The block's x is 50, so y is 51, not 6.
  • D. The outer x is still 5, not 51.

Tip: Shadowing makes a new binding; it does not change the old one.

Question 5Medium

What is printed?

let mut count = 0;
let mut inc = || count += 1;
inc();
inc();
println!("{}", count);
  1. 0
  2. 1
  3. Compile error
  4. 2

Answer: D. 2

Explanation

The closure takes a mutable borrow of count and adds 1 on each call. After the second call the borrow is no longer used, so println! can read count, which is 2.

Step-by-step solution

  1. inc captures count by mutable reference.
  2. Each inc() call adds 1, so two calls make count 2.
  3. The borrow ends after the last call to inc, so reading count is allowed.

Why the other options are wrong

  • A. The closure changes count each time it is called.
  • B. Two calls add 1 each, so the value is not 1.
  • C. Reading count after the last use of inc compiles.

Tip: A closure that changes a captured variable keeps a mutable borrow only until its last use.

Question 6Medium

What is printed?

let x: i32;
if true {
    x = 1;
} else {
    x = 2;
}
println!("{}", x);
  1. 1
  2. 2
  3. Compile error
  4. 0

Answer: A. 1

Explanation

Rust lets you declare a variable now and assign it later, once every path assigns it before it is read. Both branches assign x, and the if branch runs, so x is 1.

Step-by-step solution

  1. let x: i32; declares x without a value.
  2. Both the if and else branches assign x, so the compiler accepts the read.
  3. The if condition is true, so x = 1 runs and println! prints 1.

Why the other options are wrong

  • B. The else branch would give 2, but it never runs here.
  • C. The compiler accepts this because every path assigns x before the read.
  • D. x is assigned in every branch, so it is never 0 here.

Tip: Deferred initialisation is fine as long as every path assigns the variable before it is used.

Question 7Medium

What does this print?

let x = 5;
let x = x * 2;
{
    let x = x + 1;
}
println!("{}", x);
  1. 5
  2. 11
  3. 10
  4. 6

Answer: C. 10

Explanation

The second let shadows x with 10. The block's x (11) goes out of scope, so the outer x stays 10.

Step-by-step solution

  1. x becomes 5, then 10.
  2. Inner block makes x = 11.
  3. After the block x is 10.

Why the other options are wrong

  • A. The first shadowing doubled x.
  • B. The inner x ends with the block.
  • D. The inner shadow disappears at the closing brace.

Question 8Hard

What happens when add(255, 1) is called in a debug build?

fn add(a: u8, b: u8) -> u8 { a + b }
  1. It panics with an arithmetic overflow
  2. y is 0 silently, with the carry thrown away
  3. y is 256 because Rust widens the type
  4. Compile error in every build, whatever the values

Answer: A. It panics with an arithmetic overflow

Explanation

Debug builds check integer overflow and panic; release builds wrap by default. Use wrapping_add or checked_add to be explicit.

Step-by-step solution

  1. 255 + 1 overflows u8.
  2. Debug checks overflow.
  3. So it panics.

Why the other options are wrong

  • B. Wrapping happens only in release by default, or with wrapping_add.
  • C. u8 cannot hold 256.
  • D. Constant folding may warn, but the runtime behaviour is the panic.

Question 9Hard

What is printed?

let mut a = [1, 2, 3];
let [first, .., last] = a;
a[0] = 9;
println!("{} {} {}", first, last, a[0]);
  1. 1 3 1
  2. 9 3 9
  3. 1 3 9
  4. Compile error

Answer: C. 1 3 9

Explanation

An array of Copy values such as i32 is copied, not moved, when it is destructured. first and last are copies taken before a[0] changes, so first stays 1 and last is 3, while a[0] now prints 9.

Step-by-step solution

  1. [i32; 3] is Copy, so the pattern copies the elements it binds.
  2. first copies 1 and last copies 3 before the assignment.
  3. a[0] = 9 changes only the array, so the last value printed is 9.

Why the other options are wrong

  • A. a[0] is 9 when printed, so the last value is 9, not 1.
  • B. first was copied before the change, so it is still 1.
  • D. Arrays of Copy types are copied, not moved, so a is still usable.

Tip: Copy types are duplicated on assignment and destructuring, so later changes do not reach the copies.

Practise all 9 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics