on-internet
← Community

Obtaining a Vec from a &HashSet [closed]

Stack Overflow ·

Assume I have some function do_stuff that takes a &HashSet<X> and wants to collect the items into a Vec<X> by copying all the elements. What is the canonical way to do that? I have basically found the following ways to do this: Using _.clone().into_iter().collect() (GodBolt Link): use std::collections::HashSet; #[derive(Copy, Clone, Eq, Hash, PartialEq)] struct X {} struct Y { xs: HashSet<X>, } fn do_stuff(y: &Y) -> Vec<X> { y.xs.clone().into_iter().collect() } This is recommended here: How can I collect Vec from &HashSet? - Answer by at54321 Using _.iter().cloned().collect() (GodBolt Link): u

Comments

Sign in or sign up to comment and upvote.

No comments yet.