Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1764910

Browse files
authoredJun 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
2 parents 25245bb + b19dd14 commit 1764910

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed
 

‎core/src/iter/adapters/chain.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::ops::Try;
44

55
/// An iterator that links two iterators together, in a chain.
66
///
7-
/// This `struct` is created by [`Iterator::chain`]. See its documentation
8-
/// for more.
7+
/// This `struct` is created by [`chain`] or [`Iterator::chain`]. See their
8+
/// documentation for more.
99
///
1010
/// # Examples
1111
///
@@ -38,6 +38,39 @@ impl<A, B> Chain<A, B> {
3838
}
3939
}
4040

41+
/// Converts the arguments to iterators and links them together, in a chain.
42+
///
43+
/// See the documentation of [`Iterator::chain`] for more.
44+
///
45+
/// # Examples
46+
///
47+
/// ```
48+
/// #![feature(iter_chain)]
49+
///
50+
/// use std::iter::chain;
51+
///
52+
/// let a = [1, 2, 3];
53+
/// let b = [4, 5, 6];
54+
///
55+
/// let mut iter = chain(a, b);
56+
///
57+
/// assert_eq!(iter.next(), Some(1));
58+
/// assert_eq!(iter.next(), Some(2));
59+
/// assert_eq!(iter.next(), Some(3));
60+
/// assert_eq!(iter.next(), Some(4));
61+
/// assert_eq!(iter.next(), Some(5));
62+
/// assert_eq!(iter.next(), Some(6));
63+
/// assert_eq!(iter.next(), None);
64+
/// ```
65+
#[unstable(feature = "iter_chain", reason = "recently added", issue = "125964")]
66+
pub fn chain<A, B>(a: A, b: B) -> Chain<A::IntoIter, B::IntoIter>
67+
where
68+
A: IntoIterator,
69+
B: IntoIterator<Item = A::Item>,
70+
{
71+
Chain::new(a.into_iter(), b.into_iter())
72+
}
73+
4174
#[stable(feature = "rust1", since = "1.0.0")]
4275
impl<A, B> Iterator for Chain<A, B>
4376
where

‎core/src/iter/adapters/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pub use self::array_chunks::ArrayChunks;
4141
#[unstable(feature = "std_internals", issue = "none")]
4242
pub use self::by_ref_sized::ByRefSized;
4343

44+
#[unstable(feature = "iter_chain", reason = "recently added", issue = "125964")]
45+
pub use self::chain::chain;
46+
4447
#[stable(feature = "iter_cloned", since = "1.1.0")]
4548
pub use self::cloned::Cloned;
4649

‎core/src/iter/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ pub use self::traits::{
428428
DoubleEndedIterator, ExactSizeIterator, Extend, FromIterator, IntoIterator, Product, Sum,
429429
};
430430

431+
#[unstable(feature = "iter_chain", reason = "recently added", issue = "125964")]
432+
pub use self::adapters::chain;
431433
#[stable(feature = "iter_zip", since = "1.59.0")]
432434
pub use self::adapters::zip;
433435
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]

‎core/tests/iter/adapters/chain.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ use super::*;
22
use core::iter::*;
33
use core::num::NonZero;
44

5+
#[test]
6+
fn test_chain() {
7+
let xs = [0, 1, 2, 3, 4, 5];
8+
let ys = [30, 40, 50, 60];
9+
let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
10+
assert_eq!(Vec::from_iter(chain(xs, ys)), expected);
11+
}
12+
513
#[test]
614
fn test_iterator_chain() {
715
let xs = [0, 1, 2, 3, 4, 5];

‎core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#![feature(ip)]
7676
#![feature(iter_advance_by)]
7777
#![feature(iter_array_chunks)]
78+
#![feature(iter_chain)]
7879
#![feature(iter_collect_into)]
7980
#![feature(iter_partition_in_place)]
8081
#![feature(iter_intersperse)]

0 commit comments

Comments
 (0)
Failed to load comments.