# Join method for all iterators

**URL:** <https://internals.rust-lang.org/t/join-method-for-all-iterators/8108>\
**Category:** Uncategorized\
**Created:** [August 1, 2018, 2:41pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108 "2018-08-01T14:41:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![storyfeet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/storyfeet/32/4461_2.png) [@storyfeet](https://internals.rust-lang.org/u/storyfeet)\
**Post date:** [August 1, 2018, 2:41pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/1 "2018-08-01T14:41:28Z")

</div>

So I have recently had a situation where i had an iterator over String, and I wanted to join them with a space in the middle. it turned out to be really awkward.

I was surprised to be unable to find a join method on the iterator that did the job. I did find std::slice::SliceConcatExt But that seems to be rather specific on slices, and I didn’t want to make one.

Rather than solve the problem for my specific case, I remembered that Premature Generalization is the root of all Evil, and so of course that’s what I did.

[https://play.rust-lang.org/?gist=9e957c61251d082abe4907bfd3d75839&version=stable&mode=debug&edition=2015](https://play.rust-lang.org/?gist=9e957c61251d082abe4907bfd3d75839&version=stable&mode=debug&edition=2015)

The above is a joining iterator trait called Joinable which works on almost anything. It clones and repeats the joining parameyer and alternates with the original iterator starting and ending with values from the original iterator.

Could this / or something like it have a place in the stdlib?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [August 1, 2018, 3:02pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/2 "2018-08-01T15:02:59Z")

</div>

I agree it’s a missing functionality. There were many occasions when I wished to have `join()` on an iterator of strings.

I was thinking about implementing a collect-like method on `Iterator` of elements like `AsRef<str>` or `Display`.

Your solution is interesting. It doesn’t actually do the joining, but it’s more like interleaving.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [August 1, 2018, 3:28pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/3 "2018-08-01T15:28:11Z")

</div>

I think the itertools crate already has a join method: [https://docs.rs/itertools/0.7.2/itertools/fn.join.html](https://docs.rs/itertools/0.7.2/itertools/fn.join.html) Seems like IntoIterator is good enough for it, so I’m not sure a new Joinable trait buys us anything.

---

<div class="post-metadata">

**Author:** ![storyfeet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/storyfeet/32/4461_2.png) [@storyfeet](https://internals.rust-lang.org/u/storyfeet)\
**Post date:** [August 1, 2018, 3:29pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/4 "2018-08-01T15:29:29Z")

</div>

yeah, I pondered writing using some actual form of interleave on two separate iterators and then making this a specific case,where the iterator returns the same thing.

I think that might have been too generalized even for me.

---

<div class="post-metadata">

**Author:** ![storyfeet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/storyfeet/32/4461_2.png) [@storyfeet](https://internals.rust-lang.org/u/storyfeet)\
**Post date:** [August 1, 2018, 3:34pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/5 "2018-08-01T15:34:31Z")

</div>

ah, that’s what I was looking for lol. Coulda saved me some work there. I probably wouldn’t have bothered writing this if i’d found that first.

Still: this version chains on iterators, so that might still have value.

Maybe I’ll make it a crate instead.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [August 1, 2018, 3:44pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/6 "2018-08-01T15:44:57Z")

</div>

I’m not sure I would call your function on the playground “join”. If I understand correctly, it turns

```rust
["hello", "world", "and", "goodnight"]

into

["hello", ",", "world", ",", "and", ",", "goodnight"]

```

, right? This sounds like [`Itertools::intersperse`](https://docs.rs/itertools/0.7.2/itertools/trait.Itertools.html#method.intersperse).

---

<div class="post-metadata">

**Author:** ![storyfeet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/storyfeet/32/4461_2.png) [@storyfeet](https://internals.rust-lang.org/u/storyfeet)\
**Post date:** [August 1, 2018, 3:53pm UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/7 "2018-08-01T15:53:58Z")

</div>

yup it sounds exactly like that. and looking at the code it does the job better too.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/join-method-for-all-iterators/8108/8 "2019-03-25T08:30:37Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
