Coding challenge ( rust)

Give it a try and see if you can predict the output of this Rust program.

use std::collections::HashMap;

fn main() {
    let input_numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
    let result: Vec<_> = input_numbers
        .iter()
        .filter(|&&x| x % 2 == 0)
        .map(|x| x * x)
        .filter(|&x| x > 20)
        .fold(HashMap::new(), |mut acc, x| {
            let counter = acc.entry(x % 5).or_insert(0);
            *counter += 1;
            acc
        })
        .into_iter()
        .filter(|&(_, count)| count > 1)
        .map(|(key, _)| key)
        .collect();

    println!("{:?}", result);
}
Was this page helpful?