From 187162e9917b6255c5eb12434362e6c4cc9c6976 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 22 Sep 2020 22:27:18 +0200 Subject: [PATCH] Add missing code examples on slice iter types --- library/core/src/slice/iter.rs | 164 +++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 84fa34c75e3..1bac35845df 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -270,6 +270,13 @@ pub(super) trait SplitIter: DoubleEndedIterator { /// /// This struct is created by the [`split`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = [10, 40, 33, 20]; +/// let mut iter = slice.split(|num| num % 3 == 0); +/// ``` +/// /// [`split`]: ../../std/primitive.slice.html#method.split /// [slices]: ../../std/primitive.slice.html #[stable(feature = "rust1", since = "1.0.0")] @@ -378,6 +385,15 @@ impl FusedIterator for Split<'_, T, P> where P: FnMut(&T) -> bool {} /// /// This struct is created by the [`split_inclusive`] method on [slices]. /// +/// # Example +/// +/// ``` +/// #![feature(split_inclusive)] +/// +/// let slice = [10, 40, 33, 20]; +/// let mut iter = slice.split_inclusive(|num| num % 3 == 0); +/// ``` +/// /// [`split_inclusive`]: ../../std/primitive.slice.html#method.split_inclusive /// [slices]: ../../std/primitive.slice.html #[unstable(feature = "split_inclusive", issue = "72360")] @@ -476,6 +492,13 @@ impl FusedIterator for SplitInclusive<'_, T, P> where P: FnMut(&T) -> bool /// /// This struct is created by the [`split_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut v = [10, 40, 30, 20, 60, 50]; +/// let iter = v.split_mut(|num| *num % 3 == 0); +/// ``` +/// /// [`split_mut`]: ../../std/primitive.slice.html#method.split_mut /// [slices]: ../../std/primitive.slice.html #[stable(feature = "rust1", since = "1.0.0")] @@ -591,6 +614,15 @@ impl FusedIterator for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {} /// /// This struct is created by the [`split_inclusive_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// #![feature(split_inclusive)] +/// +/// let mut v = [10, 40, 30, 20, 60, 50]; +/// let iter = v.split_inclusive_mut(|num| *num % 3 == 0); +/// ``` +/// /// [`split_inclusive_mut`]: ../../std/primitive.slice.html#method.split_inclusive_mut /// [slices]: ../../std/primitive.slice.html #[unstable(feature = "split_inclusive", issue = "72360")] @@ -698,6 +730,13 @@ impl FusedIterator for SplitInclusiveMut<'_, T, P> where P: FnMut(&T) -> b /// /// This struct is created by the [`rsplit`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = [11, 22, 33, 0, 44, 55]; +/// let iter = slice.rsplit(|num| *num == 0); +/// ``` +/// /// [`rsplit`]: ../../std/primitive.slice.html#method.rsplit /// [slices]: ../../std/primitive.slice.html #[stable(feature = "slice_rsplit", since = "1.27.0")] @@ -770,6 +809,13 @@ impl FusedIterator for RSplit<'_, T, P> where P: FnMut(&T) -> bool {} /// /// This struct is created by the [`rsplit_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut slice = [11, 22, 33, 0, 44, 55]; +/// let iter = slice.rsplit_mut(|num| *num == 0); +/// ``` +/// /// [`rsplit_mut`]: ../../std/primitive.slice.html#method.rsplit_mut /// [slices]: ../../std/primitive.slice.html #[stable(feature = "slice_rsplit", since = "1.27.0")] @@ -875,6 +921,13 @@ impl> Iterator for GenericSplitN { /// /// This struct is created by the [`splitn`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = [10, 40, 30, 20, 60, 50]; +/// let iter = slice.splitn(2, |num| *num % 3 == 0); +/// ``` +/// /// [`splitn`]: ../../std/primitive.slice.html#method.splitn /// [slices]: ../../std/primitive.slice.html #[stable(feature = "rust1", since = "1.0.0")] @@ -901,6 +954,13 @@ where /// /// This struct is created by the [`rsplitn`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = [10, 40, 30, 20, 60, 50]; +/// let iter = slice.rsplitn(2, |num| *num % 3 == 0); +/// ``` +/// /// [`rsplitn`]: ../../std/primitive.slice.html#method.rsplitn /// [slices]: ../../std/primitive.slice.html #[stable(feature = "rust1", since = "1.0.0")] @@ -926,6 +986,13 @@ where /// /// This struct is created by the [`splitn_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut slice = [10, 40, 30, 20, 60, 50]; +/// let iter = slice.splitn_mut(2, |num| *num % 3 == 0); +/// ``` +/// /// [`splitn_mut`]: ../../std/primitive.slice.html#method.splitn_mut /// [slices]: ../../std/primitive.slice.html #[stable(feature = "rust1", since = "1.0.0")] @@ -952,6 +1019,13 @@ where /// /// This struct is created by the [`rsplitn_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut slice = [10, 40, 30, 20, 60, 50]; +/// let iter = slice.rsplitn_mut(2, |num| *num % 3 == 0); +/// ``` +/// /// [`rsplitn_mut`]: ../../std/primitive.slice.html#method.rsplitn_mut /// [slices]: ../../std/primitive.slice.html #[stable(feature = "rust1", since = "1.0.0")] @@ -981,6 +1055,13 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] } /// /// This struct is created by the [`windows`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = ['r', 'u', 's', 't']; +/// let iter = slice.windows(2); +/// ``` +/// /// [`windows`]: ../../std/primitive.slice.html#method.windows /// [slices]: ../../std/primitive.slice.html #[derive(Debug)] @@ -1113,6 +1194,13 @@ unsafe impl<'a, T> TrustedRandomAccess for Windows<'a, T> { /// /// This struct is created by the [`chunks`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.chunks(2); +/// ``` +/// /// [`chunks`]: ../../std/primitive.slice.html#method.chunks /// [slices]: ../../std/primitive.slice.html #[derive(Debug)] @@ -1267,6 +1355,13 @@ unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> { /// /// This struct is created by the [`chunks_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.chunks_mut(2); +/// ``` +/// /// [`chunks_mut`]: ../../std/primitive.slice.html#method.chunks_mut /// [slices]: ../../std/primitive.slice.html #[derive(Debug)] @@ -1419,6 +1514,13 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> { /// /// This struct is created by the [`chunks_exact`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.chunks_exact(2); +/// ``` +/// /// [`chunks_exact`]: ../../std/primitive.slice.html#method.chunks_exact /// [`remainder`]: ChunksExact::remainder /// [slices]: ../../std/primitive.slice.html @@ -1559,6 +1661,13 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksExact<'a, T> { /// /// This struct is created by the [`chunks_exact_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.chunks_exact_mut(2); +/// ``` +/// /// [`chunks_exact_mut`]: ../../std/primitive.slice.html#method.chunks_exact_mut /// [`into_remainder`]: ChunksExactMut::into_remainder /// [slices]: ../../std/primitive.slice.html @@ -1692,6 +1801,15 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> { /// /// This struct is created by the [`array_windows`] method on [slices]. /// +/// # Example +/// +/// ``` +/// #![feature(array_windows)] +/// +/// let slice = [0, 1, 2, 3]; +/// let iter = slice.array_windows::<2>(); +/// ``` +/// /// [`array_windows`]: ../../std/primitive.slice.html#method.array_windows /// [slices]: ../../std/primitive.slice.html #[derive(Debug, Clone, Copy)] @@ -1796,6 +1914,15 @@ impl ExactSizeIterator for ArrayWindows<'_, T, N> { /// /// This struct is created by the [`array_chunks`] method on [slices]. /// +/// # Example +/// +/// ``` +/// #![feature(array_chunks)] +/// +/// let slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.array_chunks::<2>(); +/// ``` +/// /// [`array_chunks`]: ../../std/primitive.slice.html#method.array_chunks /// [`remainder`]: ArrayChunks::remainder /// [slices]: ../../std/primitive.slice.html @@ -1903,6 +2030,15 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunks<'a, T, N> /// /// This struct is created by the [`array_chunks_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// #![feature(array_chunks)] +/// +/// let mut slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.array_chunks_mut::<2>(); +/// ``` +/// /// [`array_chunks_mut`]: ../../std/primitive.slice.html#method.array_chunks_mut /// [`into_remainder`]: ../../std/slice/struct.ArrayChunksMut.html#method.into_remainder /// [slices]: ../../std/primitive.slice.html @@ -2001,6 +2137,13 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunksMut<'a, T, /// /// This struct is created by the [`rchunks`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.rchunks(2); +/// ``` +/// /// [`rchunks`]: ../../std/primitive.slice.html#method.rchunks /// [slices]: ../../std/primitive.slice.html #[derive(Debug)] @@ -2151,6 +2294,13 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunks<'a, T> { /// /// This struct is created by the [`rchunks_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.rchunks_mut(2); +/// ``` +/// /// [`rchunks_mut`]: ../../std/primitive.slice.html#method.rchunks_mut /// [slices]: ../../std/primitive.slice.html #[derive(Debug)] @@ -2300,6 +2450,13 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksMut<'a, T> { /// /// This struct is created by the [`rchunks_exact`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.rchunks_exact(2); +/// ``` +/// /// [`rchunks_exact`]: ../../std/primitive.slice.html#method.rchunks_exact /// [`remainder`]: ChunksExact::remainder /// [slices]: ../../std/primitive.slice.html @@ -2445,6 +2602,13 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExact<'a, T> { /// /// This struct is created by the [`rchunks_exact_mut`] method on [slices]. /// +/// # Example +/// +/// ``` +/// let mut slice = ['l', 'o', 'r', 'e', 'm']; +/// let iter = slice.rchunks_exact_mut(2); +/// ``` +/// /// [`rchunks_exact_mut`]: ../../std/primitive.slice.html#method.rchunks_exact_mut /// [`into_remainder`]: ChunksExactMut::into_remainder /// [slices]: ../../std/primitive.slice.html