This code:
fn main() {
let text = "abcd";
for char in text.chars() {
if char == 'b' {
// skip 2 chars
}
print!("{}", char);
}
// prints `abcd`, but I want `ad`
}
prints abcd
, but I want to skip 2 chars if b
was found, so that it prints ad
. How do I do that?
I tried to put the iterator into a variable outside the loop and manipulate that iterator within the loop, but the Borrow Checker doesn't allow that.
AFAIK you can't do that with a
for
loop. You will need to desugar it by hand:Playground
If you want to keep an iterator style, you can use
std::iter::successors
(I've replaced the special char with'!'
for being more readable: