iterify

Crates.ioiterify
lib.rsiterify
version0.1.2
sourcesrc
created_at2022-07-10 21:11:55.378572
updated_at2022-07-11 23:04:35.357151
descriptionTurn any type into an iterator with closures!
homepage
repositoryhttps://github.com/BRA1L0R/iterify-rs
max_upload_size
id623322
size3,757
Pietro Tamilia (BRA1L0R)

documentation

README

Iterify Downloads

Iterate over anything with Iterify! This crates takes any type T and lets you iterate over a mutable reference, yielding any type you want.

It has similar behaviour to std::iter::successors except that Iterify lets you mutate the type in-place through a &mut reference.

Example

use iterify::Iterify;

fn main() {
    let mut num = 12;
    let collatz: Vec<_> = num
        .iterify(|num| {
            *num = match *num {
                0 | 1 => return None,
                n if n % 2 != 0 => n * 3 + 1,
                n => n / 2,
            };

            Some(*num)
        })
        .collect();

    assert_eq!(collatz, &[6, 3, 10, 5, 16, 8, 4, 2, 1]);
    assert_eq!(num, 1);
}
Commit count: 6

cargo fmt