Trait iter_python::extension_traits::Join[][src]

pub trait Join: Display {
    fn join<Iterable>(
        &self,
        iterable: Iterable
    ) -> DisplayableJoin<'_, Self, <Iterable as IntoIterator>::IntoIter>
    where
        Iterable: IntoIterator,
        <Iterable as IntoIterator>::Item: Display
, { ... } }
Expand description

separator.join(iterable) method on anything Displayable.

Note that the separator goes in first argument position (before the dot), following Python’s style.

The difference with stdlib’s .join() is that it can take a lazy sequence of Displayables, such as Itertools::join(), while also returning a lazy struct that, when Displayed, shall write each item separated by self.

Example

use ::join_lazy_fmt::*;

let sequence = format!("[{}]", ", ".join(0 .. 5));
assert_eq!(sequence, "[0, 1, 2, 3, 4]");

// Since `.join()` is lazy, this does not compute an infinite string.
let _ = ", ".join(0 ..);

const N: usize = 6;
let line = format!("+-{}-+", "-+-".join((1 .. N).map(|_| "---")));
// And the following allocates only one `String`:
let matrix = format!(
    "{line}\n{body}\n{line}\n",
    line=line,
    body="\n".join(
        (1 .. N).map(|i| lazy_format!(
            "| {row} |",
            row=" | ".join(
                (1 .. N).map(|j| lazy_format!(
                    "a{i}{j}",
                    i=i,
                    j=j,
                ))
            ),
        ))
    ),
);
assert_eq!(matrix, "\
+-----+-----+-----+-----+-----+
| a11 | a12 | a13 | a14 | a15 |
| a21 | a22 | a23 | a24 | a25 |
| a31 | a32 | a33 | a34 | a35 |
| a41 | a42 | a43 | a44 | a45 |
| a51 | a52 | a53 | a54 | a55 |
+-----+-----+-----+-----+-----+
");

Provided methods

Implementors