Question 1Easy
What is printed?
fn main() {
let v: Vec<i32> = (1..=5).rev().step_by(2).collect();
println!("{:?}", v);
}
- [5, 3, 1]
- [1, 3, 5]
- [5, 4, 3, 2, 1]
- [4, 2]
Answer: A. [5, 3, 1]
Explanation
rev() walks 5, 4, 3, 2, 1, and step_by(2) keeps every second item starting from the first: 5, 3, 1.
Step-by-step solution
- (1..=5) includes 5.
- rev() reverses the order: 5, 4, 3, 2, 1.
- step_by(2) keeps the 1st, 3rd and 5th: 5, 3, 1.
Why the other options are wrong
- B. rev() comes first, so the sequence starts at 5.
- C. step_by(2) skips every other item, so 4 and 2 are dropped.
- D. That keeps the even items; step_by starts with the first item, 5.
Tip: rev then step_by: start from the top.
Question 2Easy
What does this evaluate to?
let s: i32 = (1..=4).map(|x| x * x).sum();
- 30
- 10
- 20
- 16
Answer: A. 30
Explanation
Squares of 1..=4 are 1, 4, 9, 16 and they add up to 30.
Step-by-step solution
- 1..=4 includes 4.
- Squares: 1,4,9,16.
- Sum is 30.
Why the other options are wrong
- B. 10 is the sum without squaring.
- C. 20 misses one square.
- D. 16 is just 4 squared.
Question 3Medium
What does this code print?
let v = vec![1, 2, 3];
v.iter().map(|x| println!("{}", x));
- 1, 2 and 3 are printed on separate lines
- Nothing (iterators are lazy and map is never consumed)
- 3
- A compile error because the closure result is unused
Answer: B. Nothing (iterators are lazy and map is never consumed)
Explanation
map returns a lazy adapter; nothing happens until a consumer such as collect, sum or for_each drives it.
Step-by-step solution
- map is lazy.
- No consumer is called.
- So nothing prints.
Why the other options are wrong
- A. Without a consumer the closure never runs.
- C. No partial run either.
- D. It compiles with an unused-must-use warning.
Question 4Medium
What is printed?
fn main() {
let v: Vec<i32> = [1, 2, 3, 4]
.iter()
.scan(0, |acc, &x| { *acc += x; Some(*acc) })
.collect();
println!("{:?}", v);
}
- [1, 2, 3, 4]
- [1, 3, 6, 10]
- [10]
- [3, 5, 7]
Answer: B. [1, 3, 6, 10]
Explanation
scan keeps a running state, here acc, and yields a value at each step. Each element is added to acc and acc is yielded, giving the running totals 1, 3, 6 and 10.
Step-by-step solution
- acc starts at 0.
- Each element is added to acc, and acc is yielded: 1, 3, 6, 10.
- collect gathers those four running totals.
Why the other options are wrong
- A. map would give the original values; scan yields the running total.
- C. collect keeps every yielded value, not only the last one.
- D. That adds neighbouring pairs, but scan carries the running total forward.
Tip: scan = running state; map = no state.
Question 5Medium
What is printed?
fn main() {
let v = [2, 4, 5, 6, 8];
let a: Vec<_> = v.iter().take_while(|&&x| x % 2 == 0).collect();
let b: Vec<_> = v.iter().skip_while(|&&x| x % 2 == 0).collect();
println!("{:?} {:?}", a, b);
}
- [2, 4, 6, 8] [5]
- [5, 6, 8] [2, 4]
- [2, 4, 5] [6, 8]
- [2, 4] [5, 6, 8]
Answer: D. [2, 4] [5, 6, 8]
Explanation
take_while yields items from the start while the test holds, so it stops at 5. skip_while drops items while the test holds, then yields everything after, starting at 5.
Step-by-step solution
- Both adapters test the same even-number condition.
- take_while keeps 2 and 4, then stops at 5.
- skip_while drops 2 and 4, then yields 5, 6 and 8.
Why the other options are wrong
- A. take_while stops at the first odd number, so it never reaches 6 or 8.
- B. The two results are swapped.
- C. 5 fails the test, so take_while does not include it.
Tip: take_while stops at the first failure; skip_while starts at it.
Question 6Medium
What is printed?
fn main() {
let v: Vec<i32> = (1..=3).flat_map(|x| vec![x; x as usize]).collect();
println!("{:?}", v);
}
- [1, 2, 3]
- [1, 1, 2, 3]
- [1, 2, 2, 3, 3, 3]
- [3, 3, 3, 2, 2, 1]
Answer: C. [1, 2, 2, 3, 3, 3]
Explanation
flat_map turns each number into a vec with that many copies of itself, then flattens the results. 1 gives one 1, 2 gives two 2s, and 3 gives three 3s.
Step-by-step solution
- x = 1 makes [1].
- x = 2 makes [2, 2]; x = 3 makes [3, 3, 3].
- flat_map joins them in order: [1, 2, 2, 3, 3, 3].
Why the other options are wrong
- A. flat_map keeps every item of each inner vec, not just one.
- B. The copies are per x: 2 appears twice and 3 three times.
- D. The results are in ascending order of x, so the output is not reversed.
Tip: flat_map = map, then flatten.
Question 10Hard
What is printed?
fn main() {
let v = [(1, 'a'), (3, 'b'), (3, 'c')];
println!("{:?}", v.iter().max_by_key(|p| p.0));
}
- Some((3, 'b'))
- Some((1, 'a'))
- Some((3, 'c'))
- None
Answer: C. Some((3, 'c'))
Explanation
max_by_key returns the last element with the largest key when there is a tie. (3, 'b') and (3, 'c') both have key 3, so the later (3, 'c') is returned inside Some.
Step-by-step solution
- The largest key is 3, shared by (3, 'b') and (3, 'c').
- On a tie, max_by_key keeps the last maximum it sees.
- So the result is Some((3, 'c')).
Why the other options are wrong
- A. On a tie max_by_key keeps the later element, which is (3, 'c').
- B. (1, 'a') has the smallest key, not the largest.
- D. The iterator is not empty, so there is a result.
Tip: max_by_key keeps the last tie; min_by_key keeps the first.