0

I have a vector of a specific type. I can create a From trait that automatically converts a &str into the type.

impl From<&str> for Type {
    fn from(value: &str) -> Self {
        Self { value: value.to_string() }
    }
}

I want to have a From trait implementation that allows me to automatically convert a Vec of &str's to a Vec of these types. I tried something like this:

impl From<Vec<&str>> for Vec<Type> {
    fn from(value: Vec<&str>) -> Self {
        Self { value: todo!() }
    }
}

This does not compile because I cannot implement an external trait on an external type. Is there any other elegant way to accomplish this?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
wackazong
  • 474
  • 7
  • 18
  • I don't think it's possible. What's your usecase? `.iter().map(Into::into).collect()` should work out of the box. – Finomnis Feb 22 '23 at 15:49

0 Answers0