Vec<&T> to Vec<T>

How to get Vec, where have Vec<&T>?

There's no easy way, because &T means it's borrowed, and T means it's owned, and you can't take ownership of a thing you've borrowed — Rust doesn't support stealing! :wink:

The types are also physically laid out differently in memory, so there isn't even a way to cheat it. You'll have to copy all elements:

vec.into_iter().cloned().collect()
3 Likes

Cool! Thanks!