Step 1: Understand variable initialization and mutation
The variable count starts at 1 and is mutable.
Step 2: Calculate the new value after addition
Adding 2 to 1 gives 3, so count becomes 3.
Final Answer:
3 -> Option A
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
Step 1: Check variable mutability
Variable x is declared without mut, so it is immutable.
Step 2: Analyze reassignment attempt
Assigning x = 20; tries to change an immutable variable, causing a compile error.
Final Answer:
Cannot assign twice to immutable variable x -> Option C
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?