joinery::iter

Trait JoinableIterator

Source
pub trait JoinableIterator: Iterator + Sized {
    // Provided methods
    fn join_with<S>(self, sep: S) -> Join<CloneIterator<Self>, S>
       where Self: Clone { ... }
    fn join_concat(self) -> Join<CloneIterator<Self>, NoSeparator>
       where Self: Clone { ... }
    fn iter_join_with<S>(self, sep: S) -> JoinIter<Self, S>  { ... }
}
Expand description

A trait for converting Iterators into Join instances or JoinIter iterators.

This trait serves the same purpose as Joinable, but is implemented for Iterator types. The main difference between JoinableIterator and Joinable is that, because iterators generally don’t implement &T: IntoIterator, we need a different mechanism to allow for immutably iterating (which is required for Join’s implementation of Display).

Provided Methods§

Source

fn join_with<S>(self, sep: S) -> Join<CloneIterator<Self>, S>
where Self: Clone,

Convert a cloneable iterator into a Join instance. Whenever the Join needs to immutabley iterate over the underlying iterator (for instance, when formatting it with Display), the underlying iterator is cloned. For most iterator types this is a cheap operation, because the iterator contains just a reference to the underlying collection.

§Examples
use joinery::JoinableIterator;

let result = (0..4).map(|x| x * 2).join_with(", ").to_string();

assert_eq!(result, "0, 2, 4, 6");
Source

fn join_concat(self) -> Join<CloneIterator<Self>, NoSeparator>
where Self: Clone,

Convert a cloneable iterator into a Join instance with no separator. When formatted with Display, the elements of the iterator will be directly concatenated.

§Examples
use joinery::JoinableIterator;

let result = (0..4).map(|x| x * 2).join_concat().to_string();

assert_eq!(result, "0246");
Source

fn iter_join_with<S>(self, sep: S) -> JoinIter<Self, S>

Create an iterator which interspeses the elements of this iterator with a separator. See JoinIter for more details.

§Examples
use joinery::{JoinableIterator, JoinItem};

let mut iter = (0..3).map(|x| x * 2).iter_join_with(", ");

assert_eq!(iter.next(), Some(JoinItem::Element(0)));
assert_eq!(iter.next(), Some(JoinItem::Separator(", ")));
assert_eq!(iter.next(), Some(JoinItem::Element(2)));
assert_eq!(iter.next(), Some(JoinItem::Separator(", ")));
assert_eq!(iter.next(), Some(JoinItem::Element(4)));
assert_eq!(iter.next(), None);

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§