How do I pass an iterator I am iterating on to a function?

后端 未结 1 980

I\'m iterating through an array, and depending on the current value, I\'d like to pass the iterator to a sub function and have it deal with a number of values, and upon exit

相关标签:
1条回答
  • 2021-01-19 16:32

    The for construct consumes the iterator, and doing what you want using it will be quite tricky (if not impossible, I'm really not sure about that).

    However, you can have it working pretty easily by switching to a while let construct, like this:

    fn parse_tokens (tokens: &Vec<char>) {
        let mut iter = tokens.iter();
        let mut things: Vec<Thing> = vec![];
        while let Some(token) = iter.next() {
            match token {
                &'a' => things.push(take_three(&mut iter)),
                &'b' => things.push(take_four(&mut iter)),
                _ => {},
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题