[NEWBIE] Getting a Vec<tuple of two substrings> out of a Iterator<&str>

Sorry for these stupid questions, but I've got a hard time wrapping find the right ways in rust. I've got the following ultrasimple function:

struct Replacement<'a>(&'a str, &'a str);

fn parse_replacements<'a, T: Iterator<&'a str>>(replacements: Option<T>) -> Vec<Replacement<'a>>
{
    if let Some(replacements_iter) = replacements {
        let replacements_list: Vec<Replacement<'a>> = replacements_iter.collect().splitn(2, ':')
            .map(|r| r.expect("invalid replacement value"))
            .map(|r| Replacement(r.nth(0).unwrap(), r.nth(1).unwrap()))
            .collect();
        Some(replacements_list)
    } else {
        None
    }
}

The compiler complains:

fn parse_replacements<'a, T: Iterator<&'a str>>(replacements: Option<T>) -> Vec<Replacement<'a>>
    |                              ^^^^^^^^--------- help: remove these generics
    |                              |
    |                              expected 0 generic arguments

Why the heck ain't I allowed to say what my input type should be?

Ok, I need an

Option<Vec<Replacement<'a>>>

as return type, but that doesn't help

It needs to be T: Iterator<Item = &'a str>. Iterator doesn't have any generic parameter, but it has an associated type named Item.

2 Likes

Ok, thanks. That helped, should have remembered that. Guess I need to sleep.

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.