Eq, Hash for Vec<T>

When hashing / eq-ing on a Vec, is it guaranteed that it depends only on (1) length of vector and (2) elements of vector ?

(In particular, I'm concerned that two vectors with same length, same elements, but different CAPACITY might end up different in the eyes of eq/hash).

Yes, in fact they are implemented by passing on to the corresponding implementations for slices, where capacities are no longer relevant.

3 Likes

And this is guaranteed to remain true because Vec<T> implements Borrow<[T]>, and the Borrow trait requires that:

In particular Eq , Ord and Hash must be equivalent for borrowed and owned values: x.borrow() == y.borrow() should give the same result as x == y .

6 Likes