Rust Control Flow Quiz
45 comprehensive questions on Rust's control flow constructs, covering if expressions, loops, and control flow keywords — with 15 code examples demonstrating practical control flow usage.
Question 1
What is the primary difference between if statements in other languages and if expressions in Rust?
Question 2
What happens if you use an if expression in an assignment without an else branch?
Question 3
How do you write a multi-condition if-else if chain in Rust?
Question 4
What will this if expression evaluate to?
fn main() {
let number = if true { 5 } else { 6 };
println!("The number is: {}", number);
}Question 5
What is if let syntax used for?
Question 6
What will this code output?
fn main() {
let some_option = Some(5);
if let Some(value) = some_option {
println!("Got value: {}", value);
} else {
println!("No value");
}
}Question 7
What is the basic syntax of a loop expression?
Question 8
How do you exit a loop prematurely?
Question 9
What does the continue keyword do in a loop?
Question 10
Can loop expressions return values?
Question 11
What will this loop code output?
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 3 {
break counter * 2;
}
};
println!("Result: {}", result);
}Question 12
What is a while loop used for?
Question 13
What will this while loop do?
fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
number -= 1;
}
println!("LIFTOFF!!!");
}Question 14
What is while let syntax used for?
Question 15
What is the syntax for a for loop in Rust?
Question 16
What does 0..10 represent in a for loop?
Question 17
What will this for loop output?
fn main() {
for number in (1..4).rev() {
println!("{}!", number);
}
println!("LIFTOFF!!!");
}Question 18
When processing a list of items to find a specific value, which loop construct is most appropriate?
Question 19
What happens when you use break in a nested loop?
Question 20
How can you break out of an outer loop from within a nested inner loop?
Question 21
What will this labeled break code do?
fn main() {
'outer: loop {
println!("Entered outer loop");
'inner: loop {
println!("Entered inner loop");
break 'outer;
}
println!("This will not print");
}
println!("Exited both loops");
}Question 22
When should you use continue in a loop?
Question 23
Can continue be used with labels?
Question 24
When processing a list of items but wanting to skip invalid ones, which approach is most appropriate?
Question 25
What is the difference between break and return in a loop?
Question 26
What will this code output?
fn main() {
let mut i = 0;
loop {
i += 1;
if i == 2 {
continue;
}
if i == 4 {
break;
}
println!("i = {}", i);
}
}Question 27
How do you iterate over a vector in reverse order?
Question 28
What is the most common use case for loop expressions?
Question 29
When implementing a game loop that should run until the player quits, which loop construct is most appropriate?
Question 30
When working with Options, if let is useful for conditionally executing code when an Option is Some(value). What does this demonstrate about Rust's control flow?
Question 31
How does Rust prevent infinite loops in while loops?
Question 32
What will this code do?
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
for num in &numbers {
if *num == 3 {
break;
}
println!("num = {}", num);
}
}Question 33
When should you prefer for loops over while loops?
Question 34
What is the key difference between if and while let?
Question 35
What will this code output?
fn main() {
let condition = true;
let number = if condition {
let x = 5;
x + 1
} else {
10
};
println!("Number: {}", number);
}Question 36
How can you create a loop that runs exactly 10 times?
Question 37
What happens when you try to return a value from a loop without break?
Question 38
In a text processing application where you need to skip whitespace characters while processing a string, which control flow construct would be most appropriate?
Question 39
What will this nested loop code do?
fn main() {
for i in 1..3 {
for j in 1..3 {
if i == j {
continue;
}
println!("i={}, j={}", i, j);
}
}
}Question 40
When implementing a search algorithm that needs to examine elements until finding a match, which loop type is most efficient?
Question 41
What will this code output?
fn main() {
let mut sum = 0;
for i in 1..=5 {
if i % 2 == 0 {
continue;
}
sum += i;
}
println!("Sum: {}", sum);
}Question 42
How do you create a loop that never terminates unless explicitly broken?
Question 43
In a web server application that needs to handle requests in a loop until shutdown, which loop construct would be most appropriate?
Question 44
What will this code output?
fn main() {
let result = 'outer: loop {
for i in 1..4 {
if i == 2 {
break 'outer i * 10;
}
}
};
println!("Result: {}", result);
}Question 45
When should you use labeled loops instead of unlabeled ones?
