Rust: why std::iter::Iterator::any uses reference as closure arg?

Iterator:'s associated type IItem cannot take reference type. But why the consumer (e.g. any) method could use closure with refernce?

let a = [1, 2, 3];

assert!(a.iter().any(|&x| x > 0));

<[T]>::iter() accepts &[T] and returns an iterator yields items of type &T. In this case it accepts &[i32] and yields &i32, thus requiring you to write &x to get an i32 in the variable x.

1 Like

In rust, reference is also type?

Yes, references are types.