How to implement `From<&[String]>` for any length of `&[String; _]`?

Hi! I'm trying to implement a struct that will contain an iterator of some String array slice. Therefore, I've implemented From<&[String]> for it. But when I supply &["smth".to_string()] to some function that accepts T: Into<IterWrapper>, I'm getting errors saying

the trait `From<&[String; 1]>` is not implemented for `IterWrapper<'_>`

I've tried implementing From<&[String; _]> instead, but it's illegal syntax. Same with From<&[String; usize]>.

I'm obviously missing something, but I couldn't find anything. Playground link. Thanks in advance!

There's at least a couple of ways:

impl<'a, const N:usize> From<&'a [String;N]> for IterWrapper<'a> {
    fn from(arr: &'a [String;N]) -> Self {
        Self {
            _iterator: arr.iter(),
        }
    }
}

or

impl<'a, T> From<&'a T> for IterWrapper<'a> where T:AsRef<[String]> {
    fn from(arr: &'a T) -> Self {
        Self {
            _iterator: arr.as_ref().iter(),
        }
    }
}
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.