joinery::join

Struct Join

Source
pub struct Join<C, S> { /* private fields */ }
Expand description

The primary data structure for representing a joined sequence.

It contains a collection and a separator, and represents the elements of the collection with the separator dividing each element. A collection is defined as any type for which &T: IntoIterator; that is, any time for which references to the type are iterable.

A Join is created with Joinable::join_with, Separator::separate, or JoinableIterator::join_with. Its main use is an implementation of Display, which writes out the elements of the underlying collection, separated by the separator. It also implements IntoIterator, using a JoinIter.

§Examples

Writing via Display:

use joinery::Joinable;
use std::fmt::Write;

let content = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let join = content.join_with(", ");

let mut buffer = String::new();
write!(buffer, "Numbers: {}", join);

assert_eq!(buffer, "Numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9");

// Don't forget that `Display` gives to `ToString` for free!
assert_eq!(join.to_string(), "1, 2, 3, 4, 5, 6, 7, 8, 9")

Iterating via IntoIterator:

use joinery::{Separator, JoinItem};

let content = [0, 1, 2];
let join = ", ".separate(content);
let mut join_iter = (&join).into_iter();

assert_eq!(join_iter.next(), Some(JoinItem::Element(&0)));
assert_eq!(join_iter.next(), Some(JoinItem::Separator(&", ")));
assert_eq!(join_iter.next(), Some(JoinItem::Element(&1)));
assert_eq!(join_iter.next(), Some(JoinItem::Separator(&", ")));
assert_eq!(join_iter.next(), Some(JoinItem::Element(&2)));
assert_eq!(join_iter.next(), None);

Implementations§

Source§

impl<C, S> Join<C, S>

Source

pub fn sep(&self) -> &S

Get a reference to the separator.

Source

pub fn collection(&self) -> &C

Get a reference to the underlying collection.

Source

pub fn collection_mut(&mut self) -> &mut C

Get a mutable reference to the underlying collection

Source

pub fn into_collection(self) -> C

Consume the join, and return the underlying collection.

Source

pub fn into_parts(self) -> (C, S)

Consume self and return underlying collection and separator.

Trait Implementations§

Source§

impl<C: Clone, S: Clone> Clone for Join<C, S>

Source§

fn clone(&self) -> Join<C, S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug, S: Debug> Debug for Join<C, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C, S: Display> Display for Join<C, S>
where for<'a> &'a C: IntoIterator, for<'a> <&'a C as IntoIterator>::Item: Display<'a>,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Format the joined collection, by writing out each element of the collection, separated by the separator.

Source§

impl<'a, C, S> IntoIterator for &'a Join<C, S>

Source§

fn into_iter(self) -> Self::IntoIter

Create a referential iterator over the join. This iterator iterates over references to the underlying collection, interspersed with references to the separator. See the JoinIter documentation for more details.

Source§

type IntoIter = JoinIter<<&'a C as IntoIterator>::IntoIter, &'a S>

Which kind of iterator are we turning this into?
Source§

type Item = JoinItem<<&'a C as IntoIterator>::Item, &'a S>

The type of the elements being iterated over.
Source§

impl<C: IntoIterator, S: Clone> IntoIterator for Join<C, S>

Source§

fn into_iter(self) -> Self::IntoIter

Create a consuming iterator from a Join. This iterator consumes the elements of the underlying collection, and intersperses them with clones of the separator. See the JoinIter documentation for more details.

Source§

type IntoIter = JoinIter<<C as IntoIterator>::IntoIter, S>

Which kind of iterator are we turning this into?
Source§

type Item = JoinItem<<C as IntoIterator>::Item, S>

The type of the elements being iterated over.
Source§

impl<C: PartialEq, S: PartialEq> PartialEq for Join<C, S>

Source§

fn eq(&self, other: &Join<C, S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C: Eq, S: Eq> Eq for Join<C, S>

Source§

impl<C, S> StructuralPartialEq for Join<C, S>

Auto Trait Implementations§

§

impl<C, S> Freeze for Join<C, S>
where C: Freeze, S: Freeze,

§

impl<C, S> RefUnwindSafe for Join<C, S>

§

impl<C, S> Send for Join<C, S>
where C: Send, S: Send,

§

impl<C, S> Sync for Join<C, S>
where C: Sync, S: Sync,

§

impl<C, S> Unpin for Join<C, S>
where C: Unpin, S: Unpin,

§

impl<C, S> UnwindSafe for Join<C, S>
where C: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Joinable for T
where &'a T: for<'a> IntoIterator,

Source§

type Collection = T

Source§

fn join_with<S>(self, sep: S) -> Join<T, S>

Combine this object with a separator to create a new Join instance. Note that the separator does not have to share the same type as the iterator’s values. Read more
Source§

fn join_concat(self) -> Join<Self::Collection, NoSeparator>

Join this object with an empty separator. When rendered with Display, the underlying elements will be directly concatenated. Note that the separator, while empty, is still present, and will show up if you iterate this instance. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.