Trait IteratorExt

Source
pub trait IteratorExt {
    // Required methods
    fn map_into<U>(self) -> MapInto<Self, U>
       where Self: Sized + Iterator,
             <Self as Iterator>::Item: Into<U>;
    fn map_opt<T, U, F>(self, f: F) -> MapOpt<Self, F>
       where Self: Sized + Iterator<Item = Option<T>>,
             F: FnMut(T) -> U;
    fn map_res<F, T, U, E>(self, f: F) -> MapRes<Self, F>
       where Self: Sized + Iterator<Item = Result<T, E>>,
             F: FnMut(T) -> U;
    fn map_res_err<F, T, U, E>(self, f: F) -> MapResErr<Self, F>
       where Self: Sized + Iterator<Item = Result<T, E>>,
             F: FnMut(E) -> U;
    fn join_as_strings(self, separator: &str) -> String
       where Self: Iterator,
             <Self as Iterator>::Item: ToString;
}
Available on crate feature iterator only.
Expand description

std::iter::Iterator extensions.

Required Methods§

Source

fn map_into<U>(self) -> MapInto<Self, U>
where Self: Sized + Iterator, <Self as Iterator>::Item: Into<U>,

Transforms the items in the iterator using the Into trait to convert from T to U.

§Examples
let data: Vec<_> = vec![1_u8, 3_u8]
    .into_iter()
    .map_into::<i32>()
    .collect();
assert_eq!(data, vec![1_i32, 3_i32]);
Source

fn map_opt<T, U, F>(self, f: F) -> MapOpt<Self, F>
where Self: Sized + Iterator<Item = Option<T>>, F: FnMut(T) -> U,

Transforms the Some values in iterators of Option<T> using the given function f.

§Examples
let data: Vec<_> = vec![Some(1), None, Some(3)].into_iter().map_opt(|x| x * 2).collect();
assert_eq!(data, vec![Some(2), None, Some(6)]);
Source

fn map_res<F, T, U, E>(self, f: F) -> MapRes<Self, F>
where Self: Sized + Iterator<Item = Result<T, E>>, F: FnMut(T) -> U,

Transforms the Ok values in iterators of Result<T, E> using the given function f.

§Examples
let data = [Ok(1), Err("some error"), Ok(3)]
    .into_iter()
    .map_res(|x| x * 2)
    .collect::<Vec<_>>();
assert_eq!(data, vec![Ok(2), Err("some error"), Ok(6)]);
Source

fn map_res_err<F, T, U, E>(self, f: F) -> MapResErr<Self, F>
where Self: Sized + Iterator<Item = Result<T, E>>, F: FnMut(E) -> U,

Transforms the Err values in iterators of Result<T, E> using the given function f.

§Examples
let data = [Ok(1), Err("unexpected thing happened"), Ok(3)]
    .into_iter()
    .map_res_err(|err| format!("Oh no: {err}"))
    .collect::<Vec<_>>();
assert_eq!(data, vec![Ok(1), Err("Oh no: unexpected thing happened".to_string()), Ok(3)]);
Source

fn join_as_strings(self, separator: &str) -> String
where Self: Iterator, <Self as Iterator>::Item: ToString,

Converts each element of the iterator to a string and joins them into a single string, separated by the specified separator.

§Examples
let numbers = vec![1, 2, 3];
let sequence = numbers.into_iter().join_as_strings(", ");
assert_eq!(sequence, "1, 2, 3");

let words = vec!["hello", "world"];
let sentence = words.into_iter().join_as_strings(" - ");
assert_eq!(sentence, "hello - world");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<I> IteratorExt for I
where I: Iterator,