Repository navigation
Closed
Labels
ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)API Change Proposal is accepted (seconded with no objections)api-change-proposalA proposal to add or alter unstable APIs in the standard librariesA proposal to add or alter unstable APIs in the standard libraries
Description
Proposal
Problem statement
Add a convenient IntoIterator enabled function for chaining two iterators.
Motivation, use-cases
- A similar function already exists
core::iter::zip(added in Add function core::iter::zip rust#82917) - There are many cases where it can be clearer to use a direct function instead of using the iterator method.
- Particularly useful in the case where you want to prepend to another iterator.
- There is prior art for the utility of this in itertools::chain.
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())
}Reacted by Peter Jaszkowiak, León Orell Valerian Liehr, Joseph, Trevor Gross, Tim Kurdov and scottmcm
Reacted by Joseph and George Bateman
Metadata
Metadata
Assignees
Labels
ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)API Change Proposal is accepted (seconded with no objections)api-change-proposalA proposal to add or alter unstable APIs in the standard librariesA proposal to add or alter unstable APIs in the standard libraries
Activity
clarfonthey commentedon Jul 25, 2023
Would love to see this added to libstd. Actually was going to file an ACP if this didn't have one already.
Amanieu commentedon May 28, 2024
We discussed this in the libs-api meeting today. We're happy to accept this for the same reasons as
zip.Rollup merge of rust-lang#106186 - rossmacarthur:ft/iter-chain, r=Ama…
Rollup merge of rust-lang#106186 - rossmacarthur:ft/iter-chain, r=Ama…
Unrolled build for rust-lang#106186