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.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.