Iterators & Closures Questions with Answers

10 practice questions on Iterators & Closures (Rust) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Easy

What is printed?

fn main() {
    let v: Vec<i32> = (1..=5).rev().step_by(2).collect();
    println!("{:?}", v);
}
  1. [5, 3, 1]
  2. [1, 3, 5]
  3. [5, 4, 3, 2, 1]
  4. [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. (1..=5) includes 5.
  2. rev() reverses the order: 5, 4, 3, 2, 1.
  3. 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();
  1. 30
  2. 10
  3. 20
  4. 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. 1..=4 includes 4.
  2. Squares: 1,4,9,16.
  3. 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. 1, 2 and 3 are printed on separate lines
  2. Nothing (iterators are lazy and map is never consumed)
  3. 3
  4. 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

  1. map is lazy.
  2. No consumer is called.
  3. 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. [1, 2, 3, 4]
  2. [1, 3, 6, 10]
  3. [10]
  4. [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

  1. acc starts at 0.
  2. Each element is added to acc, and acc is yielded: 1, 3, 6, 10.
  3. 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);
}
  1. [2, 4, 6, 8] [5]
  2. [5, 6, 8] [2, 4]
  3. [2, 4, 5] [6, 8]
  4. [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

  1. Both adapters test the same even-number condition.
  2. take_while keeps 2 and 4, then stops at 5.
  3. 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. [1, 2, 3]
  2. [1, 1, 2, 3]
  3. [1, 2, 2, 3, 3, 3]
  4. [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

  1. x = 1 makes [1].
  2. x = 2 makes [2, 2]; x = 3 makes [3, 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 7Medium

What does vec![3, 1, 2].iter().max() return?

  1. Some(&3)
  2. 3
  3. Some(3)
  4. Some(&1)

Answer: A. Some(&3)

Explanation

iter().max() returns an Option<&i32>: Some(&3). The Option covers the empty-vector case.

Step-by-step solution

  1. iter yields &i32.
  2. max returns Option.
  3. Largest is 3.

Why the other options are wrong

  • B. It returns an Option.
  • C. iter() yields references, so &3 appears.
  • D. max picks the largest, not the smallest.

Question 8Medium

What is printed?

let v: Vec<i32> = (1..=6).filter(|x| x % 2 == 0).collect();
println!("{:?}", v);
  1. [1, 3, 5]
  2. [2, 4, 6]
  3. [2, 4]
  4. [1, 2, 3, 4, 5, 6]

Answer: B. [2, 4, 6]

Explanation

filter keeps evens from 1 to 6 inclusive; collect gathers them into a Vec.

Step-by-step solution

  1. Range 1..=6.
  2. Keep even numbers.
  3. Result [2,4,6].

Why the other options are wrong

  • A. Those are odd numbers.
  • C. The range is inclusive of 6.
  • D. filter keeps only the matching items.

Question 9Hard

A closure increments a captured variable: let mut n = 0; let mut inc = || n += 1;. Which closure trait does it implement at minimum?

  1. Fn
  2. FnOnce only
  3. FnMut
  4. Copy

Answer: C. FnMut

Explanation

A closure that mutates captured state needs &mut access, so it is FnMut; Fn closures only read, FnOnce closures consume captured values.

Step-by-step solution

  1. It mutates n.
  2. Repeated calls are fine.
  3. That is FnMut.

Why the other options are wrong

  • A. Fn cannot mutate captured state.
  • B. FnOnce is the weakest; this closure can be called repeatedly, so it is FnMut.
  • D. Copy is not a closure call trait.

Question 10Hard

What is printed?

fn main() {
    let v = [(1, 'a'), (3, 'b'), (3, 'c')];
    println!("{:?}", v.iter().max_by_key(|p| p.0));
}
  1. Some((3, 'b'))
  2. Some((1, 'a'))
  3. Some((3, 'c'))
  4. 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

  1. The largest key is 3, shared by (3, 'b') and (3, 'c').
  2. On a tie, max_by_key keeps the last maximum it sees.
  3. 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.

Practise all 10 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics