How important is hashing the length of a slice?

The Hash impl for [T] in the standard library first hashes the length of the slice, then each element in turn. How important is the use of the length here? If I'm implementing Hash for a type that can hold e.g. a Box<[T]>, could not hashing the length conceivably cause problems?

Adding the length helps Foo([1], [2, 3]) and Foo([1, 2], [3]) have different hashes, although that's not strictly necessary.

This caused an issue for VecDeque, which didn't want its two slices to depend on length:

1 Like

Oh, I didn't think about the interaction with tuples, interesting.

In context, I have a type like this:

enum Key {
    Seq(Box<[Key]>),
    OtherVariant,
}

and I want to impl Hash for the Seq variant in a way that allows reproducing the same hash in an incremental context, where the items aren't collected in a slice and their number is not known up front. So maybe I'll just hash the length after all the entries instead of before.