(Un)safely moving elements out of borrowed data

Temporarily moving elements out of collections A recurring pattern when working with collections is the following. Suppose I have a collection, like a Vec<T> , and suppose I would like to take an element out and replace it with a new one. Fortunately for us, holding a mutable reference is almost the same as having ownership, so we can for example use replace : fn replace_at_index < T > ( vec : &…