itertools/concat_impl.rs
1/// Combine all an iterator's elements into one element by using [`Extend`].
2///
3/// [`IntoIterator`]-enabled version of [`Itertools::concat`](crate::Itertools::concat).
4///
5/// This combinator will extend the first item with each of the rest of the
6/// items of the iterator. If the iterator is empty, the default value of
7/// `I::Item` is returned.
8///
9/// ```rust
10/// use itertools::concat;
11///
12/// let input = vec![vec![1], vec![2, 3], vec![4, 5, 6]];
13/// assert_eq!(concat(input), vec![1, 2, 3, 4, 5, 6]);
14/// ```
15pub fn concat<I>(iterable: I) -> I::Item
16where
17 I: IntoIterator,
18 I::Item: Extend<<<I as IntoIterator>::Item as IntoIterator>::Item> + IntoIterator + Default,
19{
20 iterable
21 .into_iter()
22 .reduce(|mut a, b| {
23 a.extend(b);
24 a
25 })
26 .unwrap_or_default()
27}