1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
/// Splits `iter` into consecutive chunks of `size` items, the last one possibly shorter.
///
/// Unlike [`slice::chunks`](https://doc.rust-lang.org/std/primitive.slice.html#method.chunks),
/// this consumes any `IntoIterator`, not just a slice, and owns the items instead of borrowing
/// them, so it also works on a lazily-generated or single-use iterator. A `size` of `0` produces
/// no chunks at all, since a non-empty chunk cannot hold zero items.
///
/// # Arguments
///
/// - `iter` - The items to split.
/// - `size` - How many items go in each chunk.
///
/// # Returns
///
/// The chunks, in order.
///
/// # Examples
///
/// ```
/// use helpers4::iter::chunk;
///
/// assert_eq!(chunk(1..=5, 2), vec![vec![1, 2], vec![3, 4], vec![5]]);
/// assert_eq!(chunk(Vec::<i32>::new(), 3), Vec::<Vec<i32>>::new());
/// assert_eq!(chunk(1..=3, 0), Vec::<Vec<i32>>::new());
/// ```