Skip to content

Tracking Issue for Iterator::join #75638

Closed
@pickfire

Description

@pickfire

This tracking issue is to create a more ergonomic method to join on Iterator without collect. Hopefully, we can do some optimizations on top of it but I haven't think of any case to improve it.
The feature gate for the issue is #![feature(iterator_join)].

Before

["hello", "world"].iter().collect::<Vec<_>>().join(" ")

After

["hello", "world"].iter().join(" ")

It makes use of Join under slice_concat_trait feature #27747 and have a similar behavior to Iterator::collect.
It is similar to join from itertools except that it makes use of the stuff mentioned for feature parity between Iterator and join and ergonomics to not need to do .collect::<Vec<_>>().

Technical implementation proof of concept.

pub fn join<T, Separator>(iter: impl Iterator<Item = T>, sep: Separator) -> <[T] as Join<Separator>>::Output
where
    [T]: Join<Separator>,
{
    // <[S] as std::slice::Join<&str>>
    // <[V] as std::slice::Join<&T>>
    // <[V] as std::slice::Join<&[T]>>
    Join::join(iter.collect::<Vec<T>>().as_slice(), sep)
}

Existing discussion on zulip https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/join.20on.20Iterator
Related issue #22754

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.

Steps

Unresolved Questions

  • Figure out potential performance improvements CC @lzutao
  • Add clippy lint to use .join(sep) rather than .collect::<Vec<_>>().join() after stabilization? Is it even possible when it goes across lines?

Implementation history

  • Initial Implementation: WIP (please let me do this)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-TrackedLibs issues that are tracked on the team's project board.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions