I have a situation where I know how to initialise a vector, but I don't know the exact order of those elements.
let mut vector = todo!();
for (index, element) in &hash_map {
vector[index] = element;
}
Now the problem is how to initialise the vector. The elements that it holds are not very lightweight, so I would prefer to cheat a little by creating an uninitialised vector:
let mut vector = Vec::with_capacity(size);
unsafe { vector.set_len(size) };
The problem is that when I later assign the value, I drop the previous element (which is uninitialised garbage) and panic:
vector[index] = element;
How can I assign an element to vector without triggering the default dropping behaviour?