One of the problems with this kind of method is that most people would want a panic if you did something like vec.set_index(usize::MAX-1, 0), rather than quietly trying to resize a Vec to use all of memory.
Gotcha... okay that's a good enough reason to not have it in a crate but maybe just keep it as a local trait for my use-case
Yeah, I agree as a general point, but if I eventually want a Vec and I know it'll be fully packed but out of order (and I don't want to bother trying to figure out the eventual length), the solution here is a bit more efficient, no?
Without knowing more about your problem, I don't think there's any way to know without benchmarking. This Default filling behavior may end up doing much the same work as a HashMap would do, especially if the HashMap ends up with no collisions after it's been fully populated. For now I'd go with whichever data structure makes your code simpler, and then benchmark both if it turns out to be a problem.
It's also worth noting that you can do the filling with a HashMap and then convert to a Vec, or vice versa, and there's tons of other exotic data structures out there, so if performance matters you'll have plenty of options to study.
Okay, took y'alls advice to heart and refactoring with HashMap. Though actually needed BTreeMap so it preserves insertion order when getting the final Vec.
Careful, BTreeMap doesn't preserve insertion order, it stores items in increasing order of keys (i.e. whatever is dictated by the key's Ord impl). If you want to preserve insertion order, try linked-hash-map.