Skip to content

Add core::iter::chain method #154

@rossmacarthur

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())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions