Trait internal_iterator::InternalIterator
source · pub trait InternalIterator: Sized {
type Item;
Show 30 methods
// Required method
fn try_for_each<R, F>(self, f: F) -> ControlFlow<R>
where F: FnMut(Self::Item) -> ControlFlow<R>;
// Provided methods
fn find_map<R, F>(self, f: F) -> Option<R>
where F: FnMut(Self::Item) -> Option<R> { ... }
fn all<F>(self, f: F) -> bool
where F: FnMut(Self::Item) -> bool { ... }
fn any<F>(self, f: F) -> bool
where F: FnMut(Self::Item) -> bool { ... }
fn chain<U>(
self,
other: U
) -> Chain<Self, <U as IntoInternalIterator>::IntoIter>
where U: IntoInternalIterator<Item = Self::Item> { ... }
fn cloned<'a, T>(self) -> Cloned<Self>
where Self: InternalIterator<Item = &'a T>,
T: Clone + 'a { ... }
fn collect<B>(self) -> B
where B: FromInternalIterator<Self::Item> { ... }
fn copied<'a, T>(self) -> Copied<Self>
where Self: InternalIterator<Item = &'a T>,
T: Copy + 'a { ... }
fn count(self) -> usize { ... }
fn enumerate(self) -> Enumerate<Self> { ... }
fn filter<P>(self, predicate: P) -> Filter<Self, P>
where P: FnMut(&Self::Item) -> bool { ... }
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
where F: FnMut(Self::Item) -> Option<T> { ... }
fn find<F>(self, f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item) -> bool { ... }
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, F>
where F: FnMut(Self::Item) -> U,
U: IntoInternalIterator { ... }
fn fold<B, F>(self, init: B, f: F) -> B
where F: FnMut(B, Self::Item) -> B { ... }
fn for_each<F>(self, f: F)
where F: FnMut(Self::Item) { ... }
fn inspect<F>(self, f: F) -> Inspect<Self, F>
where F: FnMut(&Self::Item) { ... }
fn last(self) -> Option<Self::Item> { ... }
fn map<F, T>(self, f: F) -> Map<Self, F>
where F: FnMut(Self::Item) -> T { ... }
fn max(self) -> Option<Self::Item>
where Self::Item: Ord { ... }
fn max_by<F>(self, compare: F) -> Option<Self::Item>
where F: FnMut(&Self::Item, &Self::Item) -> Ordering { ... }
fn max_by_key<B: Ord>(
self,
key: impl FnMut(&Self::Item) -> B
) -> Option<Self::Item> { ... }
fn min(self) -> Option<Self::Item>
where Self::Item: Ord { ... }
fn min_by<F>(self, compare: F) -> Option<Self::Item>
where F: FnMut(&Self::Item, &Self::Item) -> Ordering { ... }
fn min_by_key<B: Ord>(
self,
key: impl FnMut(&Self::Item) -> B
) -> Option<Self::Item> { ... }
fn next(self) -> Option<Self::Item> { ... }
fn nth(self, n: usize) -> Option<Self::Item> { ... }
fn position<F>(self, f: F) -> Option<usize>
where F: FnMut(Self::Item) -> bool { ... }
fn skip(self, n: usize) -> Skip<Self> { ... }
fn take(self, n: usize) -> Take<Self> { ... }
}
Expand description
Internal iterator over a collection.
Required Associated Types§
Required Methods§
sourcefn try_for_each<R, F>(self, f: F) -> ControlFlow<R>
fn try_for_each<R, F>(self, f: F) -> ControlFlow<R>
Applies function each elements of the iterator. Stops early if the
function returns ControlFlow::Break
.
let a = [1, 2, 3, 4, 5, 6];
let mut collected = Vec::new();
let result = a.iter().into_internal().try_for_each(|&x| {
collected.push(x);
if x == 4 {
ControlFlow::Break("stopped!")
} else {
ControlFlow::Continue(())
}
});
assert_eq!(collected, [1, 2, 3, 4]);
assert_eq!(result, ControlFlow::Break("stopped!"));
Provided Methods§
sourcefn find_map<R, F>(self, f: F) -> Option<R>
fn find_map<R, F>(self, f: F) -> Option<R>
Applies function to the elements of iterator and returns the first non-none result.
let a = ["lol", "two", "NaN", "4", "5"];
let parsed = a
.iter()
.into_internal()
.find_map(|x| x.parse().ok());
assert_eq!(parsed, Some(4));
sourcefn all<F>(self, f: F) -> bool
fn all<F>(self, f: F) -> bool
Tests if every element of the iterator matches the predicate.
let a = [1, 2, 3];
assert!(a.iter().into_internal().all(|&x| x > 0));
assert!(!a.iter().into_internal().all(|&x| x < 2));
sourcefn any<F>(self, f: F) -> bool
fn any<F>(self, f: F) -> bool
Tests if any element of the iterator matches the predicate.
let a = [1, 2, 3];
assert!(a.iter().into_internal().any(|&x| x == 2));
assert!(!a.iter().into_internal().any(|&x| x > 5));
sourcefn chain<U>(
self,
other: U
) -> Chain<Self, <U as IntoInternalIterator>::IntoIter>where
U: IntoInternalIterator<Item = Self::Item>,
fn chain<U>(
self,
other: U
) -> Chain<Self, <U as IntoInternalIterator>::IntoIter>where
U: IntoInternalIterator<Item = Self::Item>,
Takes two iterators and returns an iterator that first iterates over the elements of the first iterator, and then over the second one.
let a1 = [1, 2, 3];
let a2 = [4, 5, 6];
let chained = a1.iter().into_internal()
.chain(a2.iter().into_internal())
.collect::<Vec<_>>();
assert_eq!(chained, vec![&1, &2, &3, &4, &5, &6]);
sourcefn cloned<'a, T>(self) -> Cloned<Self>
fn cloned<'a, T>(self) -> Cloned<Self>
Creates an iterator yields cloned elements of the original iterator.
let a = [1, 2, 3];
let cloned = a.iter().into_internal().cloned().collect::<Vec<_>>();
assert_eq!(cloned, vec![1, 2, 3]);
sourcefn collect<B>(self) -> Bwhere
B: FromInternalIterator<Self::Item>,
fn collect<B>(self) -> Bwhere
B: FromInternalIterator<Self::Item>,
Transforms the iterator into a collection.
let a = [1, 2, 3];
let doubled = a
.iter()
.into_internal()
.map(|&x| x * 2)
.collect::<Vec<_>>();
assert_eq!(doubled, vec![2, 4, 6]);
sourcefn copied<'a, T>(self) -> Copied<Self>
fn copied<'a, T>(self) -> Copied<Self>
Creates an iterator yields copied elements of the original iterator.
let a = [1, 2, 3];
let cloned = a.iter().into_internal().copied().collect::<Vec<_>>();
assert_eq!(cloned, vec![1, 2, 3]);
sourcefn count(self) -> usize
fn count(self) -> usize
Returns the number of elements yielded by the iterator.
let a = [1, 2, 3];
assert_eq!(a.iter().into_internal().count(), 3);
sourcefn enumerate(self) -> Enumerate<Self>
fn enumerate(self) -> Enumerate<Self>
Creates an iterator that adds the index to every value of the original iterator.
let a = ['a', 'b', 'c'];
let enumerated = a.iter().into_internal().enumerate().collect::<Vec<_>>();
assert_eq!(enumerated, vec![(0, &'a'), (1, &'b'), (2, &'c')]);
sourcefn filter<P>(self, predicate: P) -> Filter<Self, P>
fn filter<P>(self, predicate: P) -> Filter<Self, P>
Creates an iterator which only yields elements matching the predicate.
let a = [0i32, 1, 2];
let positive = a.iter().into_internal().filter(|x| x.is_positive()).collect::<Vec<_>>();
assert_eq!(positive, vec![&1, &2]);
sourcefn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<T, F>(self, f: F) -> FilterMap<Self, F>
A combination of InternalIterator::filter
and
InternalIterator::map
.
let a = ["1", "two", "NaN", "four", "5"];
let parsed: Vec<_> = a
.iter()
.into_internal()
.filter_map(|x| x.parse::<i32>().ok())
.collect();
assert_eq!(parsed, vec![1, 5]);
sourcefn find<F>(self, f: F) -> Option<Self::Item>
fn find<F>(self, f: F) -> Option<Self::Item>
Returns the first element of the iterator that matches the predicate.
let a = [1, 2, 3];
assert_eq!(a.iter().into_internal().find(|&&x| x == 2), Some(&2));
assert_eq!(a.iter().into_internal().find(|&&x| x == 5), None);
sourcefn flat_map<U, F>(self, f: F) -> FlatMap<Self, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, F>
Creates and iterator which maps over the elements and flattens the resulting structure.
The provided closure is expected to return a type implementing
IntoInternalIterator
. The usual types that work with
std::iter::Iterator::flat_map
don’t work here, so you will need to
use IteratorExt::into_internal
to use regular iterators with this
function.
let a = [1, 2, 3];
let mapped = a.iter()
.into_internal()
.flat_map(|&x| [x * 10 + 2, x * 10 + 3])
.collect::<Vec<_>>();
assert_eq!(mapped, vec![12, 13, 22, 23, 32, 33]);
sourcefn fold<B, F>(self, init: B, f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
Folds every element into an accumulator by applying an operation, returning the final result.
let a = [1, 2, 3];
let sum = a.into_internal_iter().fold(0, |acc, x| acc + x);
assert_eq!(sum, 6);
sourcefn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Run the closure on each element, while passing that element on.
This can be used to inspect the values passed through the iterator while not modifying the rest of the iterator pipeline.
let a = [1, 4, 6, 3, 2];
let v = a.iter()
.into_internal()
.filter(|&x| x % 2 == 0)
.inspect(|x| println!("item: {}", x))
.map(|x| x / 2)
.collect::<Vec<_>>();
assert_eq!(v, vec![2, 3, 1]);
// also prints to stdout:
// item: 4
// item: 6
// item: 2
sourcefn last(self) -> Option<Self::Item>
fn last(self) -> Option<Self::Item>
Returns the last element.
let a = [1, 2, 3];
assert_eq!(a.iter().into_internal().last(), Some(&3));
let a = [1, 2, 3, 4, 5];
assert_eq!(a.iter().into_internal().last(), Some(&5));
sourcefn map<F, T>(self, f: F) -> Map<Self, F>
fn map<F, T>(self, f: F) -> Map<Self, F>
Transform each element in the iterator.
let a = [1, 2, 3];
let doubled = a
.iter()
.into_internal()
.map(|&x| x * 2)
.collect::<Vec<_>>();
assert_eq!(doubled, vec![2, 4, 6]);
sourcefn max(self) -> Option<Self::Item>
fn max(self) -> Option<Self::Item>
Returns the maximum element of an iterator.
let a = [1, 2, 3];
let b: Vec<u32> = Vec::new();
assert_eq!(a.iter().into_internal().max(), Some(&3));
assert_eq!(b.iter().into_internal().max(), None);
sourcefn max_by<F>(self, compare: F) -> Option<Self::Item>
fn max_by<F>(self, compare: F) -> Option<Self::Item>
Returns the maximum element of an iterator using a custom comparer function.
sourcefn max_by_key<B: Ord>(
self,
key: impl FnMut(&Self::Item) -> B
) -> Option<Self::Item>
fn max_by_key<B: Ord>( self, key: impl FnMut(&Self::Item) -> B ) -> Option<Self::Item>
Returns the element that gives the maximum value from the specified function.
sourcefn min(self) -> Option<Self::Item>
fn min(self) -> Option<Self::Item>
Returns the minimum element of an iterator.
let a = [1, 2, 3];
let b: Vec<u32> = Vec::new();
assert_eq!(a.iter().into_internal().min(), Some(&1));
assert_eq!(b.iter().into_internal().min(), None);
sourcefn min_by<F>(self, compare: F) -> Option<Self::Item>
fn min_by<F>(self, compare: F) -> Option<Self::Item>
Returns the minimum element of an iterator using a custom comparer function.
sourcefn min_by_key<B: Ord>(
self,
key: impl FnMut(&Self::Item) -> B
) -> Option<Self::Item>
fn min_by_key<B: Ord>( self, key: impl FnMut(&Self::Item) -> B ) -> Option<Self::Item>
Returns the element that gives the minimum value from the specified function.
sourcefn next(self) -> Option<Self::Item>
fn next(self) -> Option<Self::Item>
Returns the first element of the iterator.
Note that unlike Iterator::next
, this method consumes the iterator.
It is really only useful for getting the first element in the iterator,
and is called next
just for api similarity with regular iterators.
let a = [1, 2, 3];
assert_eq!(a.iter().into_internal().next(), Some(&1));
sourcefn nth(self, n: usize) -> Option<Self::Item>
fn nth(self, n: usize) -> Option<Self::Item>
Returns the n
th element of the iterator.
let a = [1, 2, 3];
assert_eq!(a.iter().into_internal().nth(1), Some(&2));
sourcefn position<F>(self, f: F) -> Option<usize>
fn position<F>(self, f: F) -> Option<usize>
Returns the index of the first element matching the predicate.
let a = [1, 2, 3];
assert_eq!(a.iter().into_internal().position(|&x| x == 2), Some(1));
assert_eq!(a.iter().into_internal().position(|&x| x == 5), None);