#iterator #traits #repeat #iteration #impl #loop

no-std for_each_repeat

Iterator::for_each that can repeat current iteration

1 unstable release

0.1.3 Jan 27, 2022
0.1.2 Jan 27, 2022
0.1.1 Jan 27, 2022
0.1.0 Jan 27, 2022

#2028 in Rust patterns

Custom license

11KB
188 lines

for_each_repeat

Ever got frustrated when you realize that the for loop you were writing...

fn foo(mut iter: impl Iterator<Item=i32>) {
    for i in iter {
        // do something...
        if i == 42 {
            // ughh
        }
    }
}

...needs to repeat the current iteration in some cases?
You may though: "Oh come on now I need to think on how to rewrite this into a while loop!"

Fear not! Cause we've got

Repeat

Import for_each_repeat::ForEachRepeat and put your for loop's body into a closure, that has to return LoopControl type. Using Repeat you are able to redo current iteration with the same iterator value.

use for_each_repeat::{ForEachRepeat, LoopControl};

fn foo(mut iter: impl Iterator<Item=i32>) {
    let _: Option<()> = iter.for_each_repeat(|i| {
        // do something...
        if i == 42 {
            // process it...
            return LoopControl::Repeat(i);
        }
        LoopControl::Continue(())
    });
}

#![no_std]

This crate can be used in no_std environment.

No runtime deps