Skip to content

Add core::iter::chain method #154

Closed
Closed

Description

@rossmacarthur

Proposal

Problem statement

Add a convenient IntoIterator enabled function for chaining two iterators.

Motivation, use-cases

for (x, y) in chain(xs, ys) {}
// vs
for (x, y) in xs.into_iter().chain(ys) {}

Some examples from the rust-lang/rust repo

infcx
    .type_implements_trait(p.def_id, [ty.into()].into_iter().chain(p.substs.iter()), cx.param_env)
    .must_apply_modulo_regions()
// vs
infcx
    .type_implements_trait(p.def_id, iter::chain([ty.into()], p.substs.iter()), cx.param_env)
    .must_apply_modulo_regions()
Some(t).into_iter().chain(slice::from_mut(u))
// vs
iter::chain([t], slice::from_mut(u)))

Solution sketches

// library/core/src/iter/adapters/chain.rs
pub fn chain<A, B>(a: A, b: B) -> Chain<A::IntoIter, B::IntoIter>
where
    A: IntoIterator,
    B: IntoIterator<Item = A::Item>,
{
    Chain::new(a.into_iter(), b.into_iter())
}

Activity

  1. clarfonthey commented on Jul 25, 2023

    @clarfonthey
    Contributor

    Would love to see this added to libstd. Actually was going to file an ACP if this didn't have one already.

  2. Amanieu commented on May 28, 2024

    @Amanieu
    Member

    We discussed this in the libs-api meeting today. We're happy to accept this for the same reasons as zip.

  3. added
    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)
    on May 28, 2024
  4. added a commit that references this issue on Jun 4, 2024
  5. added a commit that references this issue on Jun 4, 2024
  6. added a commit that references this issue on Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)api-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions