Rust Variables and Mutability Quiz
40 comprehensive questions on Rust's variable system, covering let bindings, mut keyword, shadowing, type inference, and constants — with 12 code examples demonstrating practical variable usage patterns.
Question 1
In Rust, what is the default mutability of variables declared with let?
Question 2
What happens when you try to reassign a value to an immutable variable?
Question 3
In a scenario where you're building a game loop that needs to update player positions frequently, how should you declare the position variables?
Question 4
What is variable shadowing in Rust?
Question 5
When would you typically use variable shadowing?
Question 6
What will this Rust code output?
fn main() {
let x = 5;
let x = x + 1;
let x = x * 2;
println!("The value of x is: {}", x);
}Question 7
How does Rust's type inference work?
Question 8
In a scenario where you're parsing user input that could be either an integer or a string, how should you handle the variable types?
Question 9
What is the difference between variables and constants in Rust?
Question 10
What is the correct syntax for declaring a constant in Rust?
Question 11
In a scenario where you need a value that never changes during program execution but is computed from other constants, what should you use?
Question 12
What happens when you try to use a variable before it's declared?
Question 13
How do you make a variable mutable in Rust?
Question 14
In a scenario where you're implementing a counter that gets incremented in a loop, which declaration is most appropriate?
Question 15
What will this code do?
fn main() {
let mut x = 5;
x = 10;
println!("x is {}", x);
}Question 16
When should you avoid using mutable variables?
Question 17
What is the scope of a variable declared with let inside a function?
Question 18
In a scenario where you have a configuration value that should be accessible throughout your program but never change, what should you use?
Question 19
What happens when you shadow a mutable variable with an immutable one?
Question 20
How does Rust determine the type of a variable when you don't specify it explicitly?
Question 21
In a scenario where you're processing a series of transformations on user input, which approach is most idiomatic in Rust?
Question 22
What will this code output?
fn main() {
let x = 5;
{
let x = 10;
println!("inner x: {}", x);
}
println!("outer x: {}", x);
}Question 23
When should you explicitly specify types instead of relying on inference?
Question 24
In a scenario where you're implementing a mathematical constant like PI, which declaration is most appropriate?
Question 25
What is the difference between let and const declarations?
Question 26
In a scenario where you need to track the number of iterations in a loop for debugging purposes, how should you declare the counter?
Question 27
What happens when you try to modify a constant?
Question 28
How do you declare a constant that represents the maximum number of connections in a server?
Question 29
In a scenario where you're implementing a caching system where the cache size should be configurable at compile time, what should you use?
Question 30
What will this code do?
fn main() {
let mut message = String::from("hello");
message.push_str(", world!");
println!("{}", message);
}Question 31
When should you use shadowing instead of mutating a variable?
Question 32
In a scenario where you have a user-provided string that you need to validate and then convert to uppercase, which approach is most Rust-idiomatic?
Question 33
What is the benefit of Rust's immutable-by-default approach?
Question 34
How do you declare a variable with an explicit type annotation?
Question 35
In a scenario where you're implementing a function that should accept both mutable and immutable references, how should you design the parameters?
Question 36
What will this code output?
fn main() {
let number = 42;
let number = number.to_string();
let number = number + " is the answer";
println!("{}", number);
}Question 37
When should you use constants instead of literals scattered throughout your code?
Question 38
In a scenario where you're implementing a timeout value that should be the same across multiple functions, how should you declare it?
Question 39
What is the key difference between variable shadowing and mutable variables?
Question 40
In a scenario where you're implementing a state machine where each state transformation creates a new state, which approach is most appropriate?
