IterExtra

Trait IterExtra 

Source
pub trait IterExtra: Iterator {
    // Provided methods
    fn min_by_partial_key<K: PartialOrd, F: FnMut(&Self::Item) -> K>(
        self,
        key: F,
    ) -> Option<Self::Item>
       where Self: Sized { ... }
    fn max_by_partial_key<K: PartialOrd, F: FnMut(&Self::Item) -> K>(
        self,
        key: F,
    ) -> Option<Self::Item>
       where Self: Sized { ... }
    fn collect_some_vec(self) -> Option<Vec<Self::Item>>
       where Self: Sized { ... }
    fn collect_ok_vec_or<E>(self, err: E) -> Result<Vec<Self::Item>, E>
       where Self: Sized { ... }
    fn collect_ok_vec_or_default<E: Default>(self) -> Result<Vec<Self::Item>, E>
       where Self: Sized { ... }
    fn deltas(self) -> Deltas<Self> 
       where Self: Sized,
             Self::Item: PartialEq { ... }
    fn deltas_by<F>(self, cmp_fn: F) -> DeltasBy<Self, F> 
       where Self: Sized,
             F: FnMut(&Self::Item, &Self::Item) -> Ordering { ... }
    fn deltas_by_key<K, F>(self, key_fn: F) -> DeltasByKey<Self, F> 
       where Self: Sized,
             F: FnMut(&Self::Item) -> K,
             K: PartialEq { ... }
}

Provided Methods§

Source

fn min_by_partial_key<K: PartialOrd, F: FnMut(&Self::Item) -> K>( self, key: F, ) -> Option<Self::Item>
where Self: Sized,

Returns the element that gives the minimum value from the specified function.

This method is similar to Iterator::min_by_key, but works with types that implement PartialOrd instead of Ord. When the comparison returns None (indicating incomparable values like NaN), it treats them as equal.

§Arguments
  • key - A function that extracts a key from each element for comparison
§Returns
  • Some(item) - The element that produces the minimum key value
  • None - If the iterator is empty
§Examples
use iter_extra::IterExtra;

let numbers = vec![3.2, 1.5, 2.8, 0.9];
let min = numbers.iter().min_by_partial_key(|&x| x);
assert_eq!(min, Some(&0.9));

// Works with NaN values
let with_nan = vec![1.0, f64::NAN, 2.0];
let min = with_nan.iter().min_by_partial_key(|&x| x);
assert_eq!(min, Some(&1.0));
Source

fn max_by_partial_key<K: PartialOrd, F: FnMut(&Self::Item) -> K>( self, key: F, ) -> Option<Self::Item>
where Self: Sized,

Returns the element that gives the maximum value from the specified function.

This method is similar to Iterator::max_by_key, but works with types that implement PartialOrd instead of Ord. When the comparison returns None (indicating incomparable values like NaN), it treats them as equal.

§Arguments
  • key - A function that extracts a key from each element for comparison
§Returns
  • Some(item) - The element that produces the maximum key value
  • None - If the iterator is empty
§Examples
use iter_extra::IterExtra;

let numbers = vec![3.2, 1.5, 2.8, 0.9];
let max = numbers.iter().max_by_partial_key(|&x| x);
assert_eq!(max, Some(&3.2));

// Works with NaN values
let with_nan = vec![1.0, f64::NAN, 2.0];
let max = with_nan.iter().max_by_partial_key(|&x| x);
assert_eq!(max, Some(&2.0));
Source

fn collect_some_vec(self) -> Option<Vec<Self::Item>>
where Self: Sized,

Source

fn collect_ok_vec_or<E>(self, err: E) -> Result<Vec<Self::Item>, E>
where Self: Sized,

Source

fn collect_ok_vec_or_default<E: Default>(self) -> Result<Vec<Self::Item>, E>
where Self: Sized,

Source

fn deltas(self) -> Deltas<Self>
where Self: Sized, Self::Item: PartialEq,

Returns an iterator that yields the distance from each element to its last occurrence.

For each element in the iterator, this method returns the number of elements between the current element and the previous occurrence of the same element. If the element hasn’t appeared before, it returns the current index.

§Returns

An iterator that yields usize values representing the delta for each element

§Examples
use iter_extra::IterExtra;

let items = vec!['a', 'b', 'c', 'a', 'c'];
let deltas: Vec<usize> = items.into_iter().deltas().collect();
assert_eq!(deltas, vec![0, 1, 2, 2, 1]);
Source

fn deltas_by<F>(self, cmp_fn: F) -> DeltasBy<Self, F>
where Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering,

Returns an iterator that yields the distance from each element to its last occurrence, using a custom comparison function.

Similar to deltas, but uses a custom comparison function to determine element equality. Two elements are considered equal when the comparison function returns Ordering::Equal.

§Arguments
  • cmp_fn - A function that compares two elements and returns an Ordering
§Returns

An iterator that yields usize values representing the delta for each element

§Examples
use iter_extra::IterExtra;

let items = vec![1.1, 2.2, 3.3, 1.2, 2.1];
let deltas: Vec<usize> = items.into_iter()
    .deltas_by(|a, b| a.floor().total_cmp(&b.floor()))
    .collect();
assert_eq!(deltas, vec![0, 1, 2, 2, 2]);
Source

fn deltas_by_key<K, F>(self, key_fn: F) -> DeltasByKey<Self, F>
where Self: Sized, F: FnMut(&Self::Item) -> K, K: PartialEq,

Returns an iterator that yields the distance from each element to its last occurrence, comparing elements by a key extracted from each element.

Similar to deltas, but determines element equality by comparing the keys extracted by the provided key function. Two elements are considered equal if their keys are equal.

§Arguments
  • key_fn - A function that extracts a key from each element for comparison
§Returns

An iterator that yields usize values representing the delta for each element

§Examples
use iter_extra::IterExtra;

let items = vec!["apple", "banana", "apricot", "blueberry"];
let deltas: Vec<usize> = items.into_iter()
    .deltas_by_key(|s| s.chars().next())
    .collect();
assert_eq!(deltas, vec![0, 1, 1, 1]);

Implementors§

Source§

impl<I: Iterator<Item = T>, T> IterExtra for I