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