I'm trying to convert a HashSet<String>
into a sorted vector that can then be join
ed with commas:
use std::collections::HashSet;
fn main() {
let mut hs = HashSet::<String>::new();
hs.insert(String::from("fee"));
hs.insert(String::from("fie"));
hs.insert(String::from("foo"));
hs.insert(String::from("fum"));
let mut v: Vec<&String> = hs.iter().collect();
v.sort();
println!("{}", v.join(", "));
}
This will not compile:
error[E0599]: no method named `join` found for struct `std::vec::Vec<&std::string::String>` in the current scope
--> src/main.rs:13:22
|
13 | println!("{}", v.join(", "));
| ^^^^ method not found in `std::vec::Vec<&std::string::String>`
I understand why I can't join the Vec<&String>
, but how can I convert the HashSet
to a Vec<String>
instead, so it can be joined?
The examples given in What's an idiomatic way to print an iterator separated by spaces in Rust? do not seem to apply because the iterator for Args
returns String
values, unlike the iterator for HashSet
which returns &String
.