Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add core::iter::chain method #154

Closed
rossmacarthur opened this issue Dec 28, 2022 · 2 comments
Closed

Add core::iter::chain method #154

rossmacarthur opened this issue Dec 28, 2022 · 2 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@rossmacarthur
Copy link

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())
}
@rossmacarthur rossmacarthur added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Dec 28, 2022
@clarfonthey
Copy link

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

@Amanieu
Copy link
Member

Amanieu commented May 28, 2024

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

@Amanieu Amanieu closed this as completed May 28, 2024
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label May 28, 2024
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jun 4, 2024
…nieu

Add function `core::iter::chain`

The addition of `core::iter::zip` (rust-lang#82917) set a precedent for adding plain functions for iterator adaptors. Adding `chain` makes it a little easier to `chain` two iterators.

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

There is prior art for the utility of this in [`itertools::chain`](https://docs.rs/itertools/latest/itertools/fn.chain.html).

Approved ACP rust-lang/libs-team#154
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jun 4, 2024
…nieu

Add function `core::iter::chain`

The addition of `core::iter::zip` (rust-lang#82917) set a precedent for adding plain functions for iterator adaptors. Adding `chain` makes it a little easier to `chain` two iterators.

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

There is prior art for the utility of this in [`itertools::chain`](https://docs.rs/itertools/latest/itertools/fn.chain.html).

Approved ACP rust-lang/libs-team#154
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 4, 2024
Rollup merge of rust-lang#106186 - rossmacarthur:ft/iter-chain, r=Amanieu

Add function `core::iter::chain`

The addition of `core::iter::zip` (rust-lang#82917) set a precedent for adding plain functions for iterator adaptors. Adding `chain` makes it a little easier to `chain` two iterators.

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

There is prior art for the utility of this in [`itertools::chain`](https://docs.rs/itertools/latest/itertools/fn.chain.html).

Approved ACP rust-lang/libs-team#154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants