Hey, I’m writing a lexer where I’m using
Peekable<Chars<'a>> to iterate over the input string. At some point in the code I noticed that I need to be able to peek more than once. So I tried to clone the iterator and try to iterate over the clone and avoid consuming the original, but then I got an error I didn’t understand.
let c = "".chars().peekable().cloned();
Gives the error
error[E0271]: type mismatch resolving `<std::iter::Peekable<std::str::Chars<'_>> as std::iter::Iterator>::Item == &_` --> <anon>:2:35 | 2 | let c = "".chars().peekable().cloned(); | ^^^^^^ expected char, found reference | = note: expected type `char` = note: found type `&_`
I read the error E0271 but it didn’t help me understand the problem. Can someone explain?
PS: I know I can use itertools to be able to peek multiple times ahead, which would solve my issue.