Sample questions
Question 1Easy
Which declaration makes x mutable so it can be reassigned later?
- let mut x = 5;
- let x mut = 5;
- mut let x = 5;
- 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
- Rust variables are immutable unless you add mut.
- The mut keyword goes after let and before the name.
- 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?
- const can be changed later with a plain assignment
- let is always global while const is local
- const is compile-time and needs a type
- 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
- const needs a type.
- It is compile-time.
- 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;?
- x becomes 6 because assignment always works
- x stays 5 and the assignment is silently ignored
- It panics at run time when the assignment executes
- 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
- let makes immutable bindings.
- Assignment needs mut.
- 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);
- 50 51
- 5 6
- 5 51
- 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
- The outer x is 5.
- Inside the block, a new x = 5 * 10 = 50 shadows it.
- 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);
- 0
- 1
- Compile error
- 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
- inc captures count by mutable reference.
- Each inc() call adds 1, so two calls make count 2.
- 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
- 2
- Compile error
- 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
- let x: i32; declares x without a value.
- Both the if and else branches assign x, so the compiler accepts the read.
- 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);
- 5
- 11
- 10
- 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
- x becomes 5, then 10.
- Inner block makes x = 11.
- 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 }
- It panics with an arithmetic overflow
- y is 0 silently, with the carry thrown away
- y is 256 because Rust widens the type
- 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
- 255 + 1 overflows u8.
- Debug checks overflow.
- 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 3 1
- 9 3 9
- 1 3 9
- 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
- [i32; 3] is Copy, so the pattern copies the elements it binds.
- first copies 1 and last copies 3 before the assignment.
- 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 →