let mut x = vec![1,2,3,4,5,6];
// let all = x.iter().all(|val: &i32| val>=1);
let all = x.iter().all(|&val| val>=1);
assert_eq!(all, true);
If u see above code, I'm calling an all method in the Iterator trait, and its working fine, but the problem is with the closure inside the all method. If you see the comment line(2nd line), the type of val variable is &i32 and, usually, we can deref with * operator, but in this code (line3)
.all(|&val| val>=1);
they used a reference to dereference the val variable.
that's not dereferencing the variable, that's the pattern to bind the variable. a single identifier is the most common pattern for variable bindings, but bindings to variable is not limited to a simple identifier. in this particular case, it's a reference pattern: