I'm a very beginner to Rust and I got confused by Rust functional programming style code. Assume freq
is a vector containing non negative integers. The below code makes me confused.
Get the largest index with a positive value.
let key_max = (0..denom).rev().step_by(1).filter(|&i| freq[i] > 0).next().unwrap();
Get the number of positive values in freq
.
let size = freq.iter().filter(|&&f| f > 0u32).count();
I wonder
- why the first line of code use
&i
(a single&
) while the second line of code uses&&f
(double&
). - In general, why do we need
&&f
in the second line of code? Why doesn't Rust make it simple to require only a single&
?