I'm attempting to write a function that is basically as follows:
fn parse_identifier(test_string: impl Iterator<Item = char> + DoubleEndedIterator, reverse: bool) -> Option<String> {
let test_string = if reverse { test_string.rev() } else { test_string };
for c in test_string {
// Do some parsing logic also slightly impacted by whether reverse is set
}
String::new() // New String built in the for loop
}
I receive an error from the compiler which is obvious in retrospect considering how Rust iterators work:
`if` and `else` have incompatible types
expected type `Rev<impl Iterator<Item = char> + DoubleEndedIterator>`
found type parameter `impl Iterator<Item = char> + DoubleEndedIterator`rustc(E0308)
Is there a way to handle this cleanly? The bulk of the logic between the two processes (reverse and !reverse) are the same so it feels painful to handle them completely separately.