Skip to content

Commit 63fdc9a

Browse files
authored
Rollup merge of #105858 - scottmcm:extra-as-chunks-example, r=the8472
Another `as_chunks` example I really liked this structure that dtolney brought up in #105316, so wanted to put it in the docs to help others use it.
2 parents 08ecb91 + a37d421 commit 63fdc9a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

library/core/src/slice/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,17 @@ impl<T> [T] {
10021002
/// assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]);
10031003
/// assert_eq!(remainder, &['m']);
10041004
/// ```
1005+
///
1006+
/// If you expect the slice to be an exact multiple, you can combine
1007+
/// `let`-`else` with an empty slice pattern:
1008+
/// ```
1009+
/// #![feature(slice_as_chunks)]
1010+
/// let slice = ['R', 'u', 's', 't'];
1011+
/// let (chunks, []) = slice.as_chunks::<2>() else {
1012+
/// panic!("slice didn't have even length")
1013+
/// };
1014+
/// assert_eq!(chunks, &[['R', 'u'], ['s', 't']]);
1015+
/// ```
10051016
#[unstable(feature = "slice_as_chunks", issue = "74985")]
10061017
#[inline]
10071018
#[must_use]

0 commit comments

Comments
 (0)