Question about pop()

Can someone demonstrate how to pop() elements from one vector and create a new vector using these elements that have been popped?

There’s likely a better API for what you need but to answer directly:

let mut src = vec![...];
let mut dst = vec![];
while let Some(v) = src.pop() { dst.push(v); }

If you elaborate on your scenario, we can see if there’s a better way.

2 Likes

Since you said "pop" as opposed to "remove", I'll assume they're at the end, at which point split_off does exactly what you want.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.