Idiomatic way to move a `Vec` into 1 of its items by an index (dropping the rest of the item except one at index)?
⚓ Rust 📅 2025-07-02 👤 surdeus 👁️ 20Given a variable vec of type Vec<Item> where Item is not Copy,
Simply writing vec[index] would cause error:
return vec[index]; // error: cannot move out of `vec`
So I must wrote this convoluted code:
vec.truncate(index);
return vec.pop().unwrap();
This looks inefficient and stupid.
Is there a better way? (I don't need the rest of vec after I have vec[index])
11 posts - 8 participants
🏷️ rust_feed