Recall & Review
beginner
What is a for loop in Rust?
A for loop in Rust is a control flow statement that repeats a block of code for each item in a collection or range.
Click to reveal answer
beginner
How do you write a for loop to print numbers 1 to 5 in Rust?
You use a range with the syntax:
for i in 1..=5 {
println!("{}", i);
} This prints numbers from 1 to 5 inclusive.Click to reveal answer
beginner
What does the range
1..5 mean in a Rust for loop?The range
1..5 means numbers from 1 up to but not including 5, so it includes 1, 2, 3, and 4.Click to reveal answer
intermediate
Can you use a for loop to iterate over elements in a vector in Rust?
Yes, you can loop over a vector like this: <pre>let v = vec![10, 20, 30];
for val in &v {
println!("{}", val);
}</pre> This prints each element in the vector.Click to reveal answer
beginner
What happens if you use
for i in 0..=3 in Rust?The loop runs with i = 0, 1, 2, 3 because
..= means inclusive range including the last number.Click to reveal answer
What does the Rust for loop
for i in 1..4 do?✗ Incorrect
The range 1..4 includes 1, 2, and 3 but excludes 4.
How do you loop over all elements in a vector
v in Rust?✗ Incorrect
Using &v borrows the vector and lets you iterate over its elements safely.
What symbol is used for an inclusive range in Rust?
✗ Incorrect
The inclusive range uses ..= to include the last number.
Which of these is a valid for loop in Rust?
✗ Incorrect
Rust uses for i in range syntax, not C-style for loops.
What will this code print?
for x in 3..=5 {
println!("{}", x);
}✗ Incorrect
The inclusive range 3..=5 includes 3, 4, and 5.
Explain how a for loop works in Rust and give an example using a range.
Think about how you count numbers one by one.
You got /4 concepts.
Describe how to loop over elements in a vector in Rust and why borrowing is important.
Remember Rust’s ownership rules.
You got /4 concepts.