Skip to content

Commit

Permalink
Rollup merge of rust-lang#105858 - scottmcm:extra-as-chunks-example, …
Browse files Browse the repository at this point in the history
…r=the8472

Another `as_chunks` example

I really liked this structure that dtolney brought up in rust-lang#105316, so wanted to put it in the docs to help others use it.
  • Loading branch information
matthiaskrgr authored Dec 18, 2022
2 parents 08ecb91 + a37d421 commit 63fdc9a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,17 @@ impl<T> [T] {
/// assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]);
/// assert_eq!(remainder, &['m']);
/// ```
///
/// If you expect the slice to be an exact multiple, you can combine
/// `let`-`else` with an empty slice pattern:
/// ```
/// #![feature(slice_as_chunks)]
/// let slice = ['R', 'u', 's', 't'];
/// let (chunks, []) = slice.as_chunks::<2>() else {
/// panic!("slice didn't have even length")
/// };
/// assert_eq!(chunks, &[['R', 'u'], ['s', 't']]);
/// ```
#[unstable(feature = "slice_as_chunks", issue = "74985")]
#[inline]
#[must_use]
Expand Down

0 comments on commit 63fdc9a

Please sign in to comment.