Exerpad
🔥0
0 XP
Lv.1 Beginner
Log In

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:

text
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);
}