Trait iter_scan::IterScan

source ·
pub trait IterScan: Iterator + Sized {
    fn scan_clone<Compute, State>(
        self,
        initial: State,
        compute: Compute
    ) -> Scan<Self, fn(_: State) -> (State, State), Compute, State>
    where
        State: Clone,
        Compute: FnMut(State, Self::Item) -> State
, { ... } fn scan_copy<Compute, State>(
        self,
        initial: State,
        compute: Compute
    ) -> Scan<Self, fn(_: State) -> (State, State), Compute, State>
    where
        State: Copy,
        Compute: FnMut(State, Self::Item) -> State
, { ... } fn scan_with_tuple<Compute, State>(
        self,
        initial: State,
        compute: Compute
    ) -> Scan<Self, fn(_: (State, State)) -> (State, State), Compute, State>
    where
        Compute: FnMut(State, Self::Item) -> (State, State)
, { ... } }
Expand description

Iterator scan methods that don’t suck.

Provided Methods§

This iterator adapter holds an internal state and emit this state on each iteration.

This internal state can be cloned.

scan_clone() takes 2 arguments:

  • An initial value which seeds the internal state.
  • A closure that:
    • Takes 2 arguments: Copy of the internal state from the previous iteration and the current item.
    • Returns the new state for the next iteration.

Example: Basic usage

use iter_scan::IterScan;
let input = ['a', 'b', 'c', 'd', 'e', 'f'];
let output: Vec<_> = input
    .into_iter()
    .scan_clone(String::new(), |acc, x| format!("{acc}{x}"))
    .collect();
assert_eq!(output, ["a", "ab", "abc", "abcd", "abcde", "abcdef"]);

This iterator adapter holds an internal state and emit this state on each iteration.

This internal state can be copied.

scan_copy() takes 2 arguments:

  • An initial value which seeds the internal state.
  • A closure that:
    • Takes 2 arguments: Copy of the internal state from the previous iteration and the current item.
    • Returns the new state for the next iteration.

Example: Basic usage

use iter_scan::IterScan;
let input = [2, 3, 4, 5];
let output: Vec<u64> = input
    .into_iter()
    .scan_copy(1, |acc, x| acc * x)
    .collect();
assert_eq!(output, [2, 6, 24, 120]);

This iterator adapter holds an internal state and emit this state on each iteration.

This adapter should be used when the internal state can neither be cloned nor copied.

scan_with_tuple() takes 2 arguments:

  • An initial value which seeds the internal state.
  • A closure that:
    • Takes 2 arguments: Copy of the internal state from the previous iteration and the current item.
    • Returns a tuple of the new state and its identical copy.

Implementors§