Exerpad
🔥 0
0 XP
Exercise 9 · Chapter: Closures & IteratorsRequired+50 XP

Fix the Iterator

This code tries to sum only the even numbers, but it's missing .iter() and the filter condition is wrong! Fix it.

Fix the code so it prints:

Sum of evens: 12

Hint: You need .iter() before .filter(), and the condition should check for even numbers (*x % 2 == 0).

rust
fn main() {
let numbers = vec![1, 2, 3, 4, 5, 6];
let sum: i32 = numbers.filter(|x| *x % 2 != 0).sum();
println!("Sum of evens: {}", sum);
}
Output
Run your code to see the output here.