Request: Convert (From/Into) between collection types that have convertible keys and values

Given type Foo and type Bar where Foo: Into<Bar>, I would like to have IntoIterator<Item = Foo> to implement Into<BTreeSet<Bar>>, so that I simplify this:

let set: BTreeSet<Bar> = btreeset! { "abc".into(), "def".into(), "ghi".into() };

into this:

let set: BTreeSet<Bar> = btreeset! { "abc", "def", "ghi" }.into();

This feature should be available for BTreeMap, HashSet, and HashMap too.

This currently isn't possible because it would overlap with the impl of From<T> for T. I think you'll have to make do with something like set.into_iter().map(Into::into).collect().

1 Like

It is not too useful to build the first BTreeSet if all you are doing is to iterate over it.

let set: BTreeSet<String> = [
    "a",
    "long",
    "list",
    "containing",
    "several",
    "string",
    "literals",
]
.iter()
.map(|&x| x.into())
.collect();
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.