I noticed this pattern in a codebase I was working with.
let a = vec![1, 2, 3];
let b = vec![4, 5, 6];
// The below three are equivalent
a.iter().zip(b.iter()).all(|(x, y)| x == y);
a.iter().eq(b.iter());
a.eq(&b);
As long as a.len() == b.len()
, this should work. Am I missing something here? If the logic is sound, would it make sense to add this as a lint to clippy?
Iām not sure how a clippy lint could notice the a.len() == b.len()
case.
The latter 2 should indeed be equivalent though, and the latter should always be better; for example it can do a proper up-front length check.
So it seems resonable to lint against usage of Iterator::eq
on (newly-created) slice and/or vector iterators.
4 Likes
The first one seems to check if either is a prefix of the other, which is distinct and something you may actually want to do.
3 Likes
Yes indeed, which is why the constraint of both vectors having equal length is important for this reduction.