Reference to filter map method

Hi,

I am trying an example

    let arr = [
        2, 4, 87, 99, 323, 4, 5, 6, 7, 8, 33, 43, 232
    ];

    let collection: Vec<_> = arr
        .iter()
        .enumerate()
        .filter_map(|(i, elm)| if i % 2 != 0 { Some(elm) } else { None })
        .collect();
    for elm in collection {
        println!("{}", elm);
    }

The arguments for filter_map has to be &(i, elm) but why is this not a reference? Since I am
iterating over &T.

The argument for filter_map is not a reference like for filter. And enumerate() gives you (index, element) tuples where element is the &i32 reference in your case.

1 Like

So it gives (i, &elm).