Skip to content

Commit 187162e

Browse files
Add missing code examples on slice iter types
1 parent 953f33c commit 187162e

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

library/core/src/slice/iter.rs

+164
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ pub(super) trait SplitIter: DoubleEndedIterator {
270270
///
271271
/// This struct is created by the [`split`] method on [slices].
272272
///
273+
/// # Example
274+
///
275+
/// ```
276+
/// let slice = [10, 40, 33, 20];
277+
/// let mut iter = slice.split(|num| num % 3 == 0);
278+
/// ```
279+
///
273280
/// [`split`]: ../../std/primitive.slice.html#method.split
274281
/// [slices]: ../../std/primitive.slice.html
275282
#[stable(feature = "rust1", since = "1.0.0")]
@@ -378,6 +385,15 @@ impl<T, P> FusedIterator for Split<'_, T, P> where P: FnMut(&T) -> bool {}
378385
///
379386
/// This struct is created by the [`split_inclusive`] method on [slices].
380387
///
388+
/// # Example
389+
///
390+
/// ```
391+
/// #![feature(split_inclusive)]
392+
///
393+
/// let slice = [10, 40, 33, 20];
394+
/// let mut iter = slice.split_inclusive(|num| num % 3 == 0);
395+
/// ```
396+
///
381397
/// [`split_inclusive`]: ../../std/primitive.slice.html#method.split_inclusive
382398
/// [slices]: ../../std/primitive.slice.html
383399
#[unstable(feature = "split_inclusive", issue = "72360")]
@@ -476,6 +492,13 @@ impl<T, P> FusedIterator for SplitInclusive<'_, T, P> where P: FnMut(&T) -> bool
476492
///
477493
/// This struct is created by the [`split_mut`] method on [slices].
478494
///
495+
/// # Example
496+
///
497+
/// ```
498+
/// let mut v = [10, 40, 30, 20, 60, 50];
499+
/// let iter = v.split_mut(|num| *num % 3 == 0);
500+
/// ```
501+
///
479502
/// [`split_mut`]: ../../std/primitive.slice.html#method.split_mut
480503
/// [slices]: ../../std/primitive.slice.html
481504
#[stable(feature = "rust1", since = "1.0.0")]
@@ -591,6 +614,15 @@ impl<T, P> FusedIterator for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {}
591614
///
592615
/// This struct is created by the [`split_inclusive_mut`] method on [slices].
593616
///
617+
/// # Example
618+
///
619+
/// ```
620+
/// #![feature(split_inclusive)]
621+
///
622+
/// let mut v = [10, 40, 30, 20, 60, 50];
623+
/// let iter = v.split_inclusive_mut(|num| *num % 3 == 0);
624+
/// ```
625+
///
594626
/// [`split_inclusive_mut`]: ../../std/primitive.slice.html#method.split_inclusive_mut
595627
/// [slices]: ../../std/primitive.slice.html
596628
#[unstable(feature = "split_inclusive", issue = "72360")]
@@ -698,6 +730,13 @@ impl<T, P> FusedIterator for SplitInclusiveMut<'_, T, P> where P: FnMut(&T) -> b
698730
///
699731
/// This struct is created by the [`rsplit`] method on [slices].
700732
///
733+
/// # Example
734+
///
735+
/// ```
736+
/// let slice = [11, 22, 33, 0, 44, 55];
737+
/// let iter = slice.rsplit(|num| *num == 0);
738+
/// ```
739+
///
701740
/// [`rsplit`]: ../../std/primitive.slice.html#method.rsplit
702741
/// [slices]: ../../std/primitive.slice.html
703742
#[stable(feature = "slice_rsplit", since = "1.27.0")]
@@ -770,6 +809,13 @@ impl<T, P> FusedIterator for RSplit<'_, T, P> where P: FnMut(&T) -> bool {}
770809
///
771810
/// This struct is created by the [`rsplit_mut`] method on [slices].
772811
///
812+
/// # Example
813+
///
814+
/// ```
815+
/// let mut slice = [11, 22, 33, 0, 44, 55];
816+
/// let iter = slice.rsplit_mut(|num| *num == 0);
817+
/// ```
818+
///
773819
/// [`rsplit_mut`]: ../../std/primitive.slice.html#method.rsplit_mut
774820
/// [slices]: ../../std/primitive.slice.html
775821
#[stable(feature = "slice_rsplit", since = "1.27.0")]
@@ -875,6 +921,13 @@ impl<T, I: SplitIter<Item = T>> Iterator for GenericSplitN<I> {
875921
///
876922
/// This struct is created by the [`splitn`] method on [slices].
877923
///
924+
/// # Example
925+
///
926+
/// ```
927+
/// let slice = [10, 40, 30, 20, 60, 50];
928+
/// let iter = slice.splitn(2, |num| *num % 3 == 0);
929+
/// ```
930+
///
878931
/// [`splitn`]: ../../std/primitive.slice.html#method.splitn
879932
/// [slices]: ../../std/primitive.slice.html
880933
#[stable(feature = "rust1", since = "1.0.0")]
@@ -901,6 +954,13 @@ where
901954
///
902955
/// This struct is created by the [`rsplitn`] method on [slices].
903956
///
957+
/// # Example
958+
///
959+
/// ```
960+
/// let slice = [10, 40, 30, 20, 60, 50];
961+
/// let iter = slice.rsplitn(2, |num| *num % 3 == 0);
962+
/// ```
963+
///
904964
/// [`rsplitn`]: ../../std/primitive.slice.html#method.rsplitn
905965
/// [slices]: ../../std/primitive.slice.html
906966
#[stable(feature = "rust1", since = "1.0.0")]
@@ -926,6 +986,13 @@ where
926986
///
927987
/// This struct is created by the [`splitn_mut`] method on [slices].
928988
///
989+
/// # Example
990+
///
991+
/// ```
992+
/// let mut slice = [10, 40, 30, 20, 60, 50];
993+
/// let iter = slice.splitn_mut(2, |num| *num % 3 == 0);
994+
/// ```
995+
///
929996
/// [`splitn_mut`]: ../../std/primitive.slice.html#method.splitn_mut
930997
/// [slices]: ../../std/primitive.slice.html
931998
#[stable(feature = "rust1", since = "1.0.0")]
@@ -952,6 +1019,13 @@ where
9521019
///
9531020
/// This struct is created by the [`rsplitn_mut`] method on [slices].
9541021
///
1022+
/// # Example
1023+
///
1024+
/// ```
1025+
/// let mut slice = [10, 40, 30, 20, 60, 50];
1026+
/// let iter = slice.rsplitn_mut(2, |num| *num % 3 == 0);
1027+
/// ```
1028+
///
9551029
/// [`rsplitn_mut`]: ../../std/primitive.slice.html#method.rsplitn_mut
9561030
/// [slices]: ../../std/primitive.slice.html
9571031
#[stable(feature = "rust1", since = "1.0.0")]
@@ -981,6 +1055,13 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] }
9811055
///
9821056
/// This struct is created by the [`windows`] method on [slices].
9831057
///
1058+
/// # Example
1059+
///
1060+
/// ```
1061+
/// let slice = ['r', 'u', 's', 't'];
1062+
/// let iter = slice.windows(2);
1063+
/// ```
1064+
///
9841065
/// [`windows`]: ../../std/primitive.slice.html#method.windows
9851066
/// [slices]: ../../std/primitive.slice.html
9861067
#[derive(Debug)]
@@ -1113,6 +1194,13 @@ unsafe impl<'a, T> TrustedRandomAccess for Windows<'a, T> {
11131194
///
11141195
/// This struct is created by the [`chunks`] method on [slices].
11151196
///
1197+
/// # Example
1198+
///
1199+
/// ```
1200+
/// let slice = ['l', 'o', 'r', 'e', 'm'];
1201+
/// let iter = slice.chunks(2);
1202+
/// ```
1203+
///
11161204
/// [`chunks`]: ../../std/primitive.slice.html#method.chunks
11171205
/// [slices]: ../../std/primitive.slice.html
11181206
#[derive(Debug)]
@@ -1267,6 +1355,13 @@ unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> {
12671355
///
12681356
/// This struct is created by the [`chunks_mut`] method on [slices].
12691357
///
1358+
/// # Example
1359+
///
1360+
/// ```
1361+
/// let mut slice = ['l', 'o', 'r', 'e', 'm'];
1362+
/// let iter = slice.chunks_mut(2);
1363+
/// ```
1364+
///
12701365
/// [`chunks_mut`]: ../../std/primitive.slice.html#method.chunks_mut
12711366
/// [slices]: ../../std/primitive.slice.html
12721367
#[derive(Debug)]
@@ -1419,6 +1514,13 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> {
14191514
///
14201515
/// This struct is created by the [`chunks_exact`] method on [slices].
14211516
///
1517+
/// # Example
1518+
///
1519+
/// ```
1520+
/// let slice = ['l', 'o', 'r', 'e', 'm'];
1521+
/// let iter = slice.chunks_exact(2);
1522+
/// ```
1523+
///
14221524
/// [`chunks_exact`]: ../../std/primitive.slice.html#method.chunks_exact
14231525
/// [`remainder`]: ChunksExact::remainder
14241526
/// [slices]: ../../std/primitive.slice.html
@@ -1559,6 +1661,13 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksExact<'a, T> {
15591661
///
15601662
/// This struct is created by the [`chunks_exact_mut`] method on [slices].
15611663
///
1664+
/// # Example
1665+
///
1666+
/// ```
1667+
/// let mut slice = ['l', 'o', 'r', 'e', 'm'];
1668+
/// let iter = slice.chunks_exact_mut(2);
1669+
/// ```
1670+
///
15621671
/// [`chunks_exact_mut`]: ../../std/primitive.slice.html#method.chunks_exact_mut
15631672
/// [`into_remainder`]: ChunksExactMut::into_remainder
15641673
/// [slices]: ../../std/primitive.slice.html
@@ -1692,6 +1801,15 @@ unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> {
16921801
///
16931802
/// This struct is created by the [`array_windows`] method on [slices].
16941803
///
1804+
/// # Example
1805+
///
1806+
/// ```
1807+
/// #![feature(array_windows)]
1808+
///
1809+
/// let slice = [0, 1, 2, 3];
1810+
/// let iter = slice.array_windows::<2>();
1811+
/// ```
1812+
///
16951813
/// [`array_windows`]: ../../std/primitive.slice.html#method.array_windows
16961814
/// [slices]: ../../std/primitive.slice.html
16971815
#[derive(Debug, Clone, Copy)]
@@ -1796,6 +1914,15 @@ impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
17961914
///
17971915
/// This struct is created by the [`array_chunks`] method on [slices].
17981916
///
1917+
/// # Example
1918+
///
1919+
/// ```
1920+
/// #![feature(array_chunks)]
1921+
///
1922+
/// let slice = ['l', 'o', 'r', 'e', 'm'];
1923+
/// let iter = slice.array_chunks::<2>();
1924+
/// ```
1925+
///
17991926
/// [`array_chunks`]: ../../std/primitive.slice.html#method.array_chunks
18001927
/// [`remainder`]: ArrayChunks::remainder
18011928
/// [slices]: ../../std/primitive.slice.html
@@ -1903,6 +2030,15 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunks<'a, T, N>
19032030
///
19042031
/// This struct is created by the [`array_chunks_mut`] method on [slices].
19052032
///
2033+
/// # Example
2034+
///
2035+
/// ```
2036+
/// #![feature(array_chunks)]
2037+
///
2038+
/// let mut slice = ['l', 'o', 'r', 'e', 'm'];
2039+
/// let iter = slice.array_chunks_mut::<2>();
2040+
/// ```
2041+
///
19062042
/// [`array_chunks_mut`]: ../../std/primitive.slice.html#method.array_chunks_mut
19072043
/// [`into_remainder`]: ../../std/slice/struct.ArrayChunksMut.html#method.into_remainder
19082044
/// [slices]: ../../std/primitive.slice.html
@@ -2001,6 +2137,13 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccess for ArrayChunksMut<'a, T,
20012137
///
20022138
/// This struct is created by the [`rchunks`] method on [slices].
20032139
///
2140+
/// # Example
2141+
///
2142+
/// ```
2143+
/// let slice = ['l', 'o', 'r', 'e', 'm'];
2144+
/// let iter = slice.rchunks(2);
2145+
/// ```
2146+
///
20042147
/// [`rchunks`]: ../../std/primitive.slice.html#method.rchunks
20052148
/// [slices]: ../../std/primitive.slice.html
20062149
#[derive(Debug)]
@@ -2151,6 +2294,13 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunks<'a, T> {
21512294
///
21522295
/// This struct is created by the [`rchunks_mut`] method on [slices].
21532296
///
2297+
/// # Example
2298+
///
2299+
/// ```
2300+
/// let mut slice = ['l', 'o', 'r', 'e', 'm'];
2301+
/// let iter = slice.rchunks_mut(2);
2302+
/// ```
2303+
///
21542304
/// [`rchunks_mut`]: ../../std/primitive.slice.html#method.rchunks_mut
21552305
/// [slices]: ../../std/primitive.slice.html
21562306
#[derive(Debug)]
@@ -2300,6 +2450,13 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksMut<'a, T> {
23002450
///
23012451
/// This struct is created by the [`rchunks_exact`] method on [slices].
23022452
///
2453+
/// # Example
2454+
///
2455+
/// ```
2456+
/// let slice = ['l', 'o', 'r', 'e', 'm'];
2457+
/// let iter = slice.rchunks_exact(2);
2458+
/// ```
2459+
///
23032460
/// [`rchunks_exact`]: ../../std/primitive.slice.html#method.rchunks_exact
23042461
/// [`remainder`]: ChunksExact::remainder
23052462
/// [slices]: ../../std/primitive.slice.html
@@ -2445,6 +2602,13 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExact<'a, T> {
24452602
///
24462603
/// This struct is created by the [`rchunks_exact_mut`] method on [slices].
24472604
///
2605+
/// # Example
2606+
///
2607+
/// ```
2608+
/// let mut slice = ['l', 'o', 'r', 'e', 'm'];
2609+
/// let iter = slice.rchunks_exact_mut(2);
2610+
/// ```
2611+
///
24482612
/// [`rchunks_exact_mut`]: ../../std/primitive.slice.html#method.rchunks_exact_mut
24492613
/// [`into_remainder`]: ChunksExactMut::into_remainder
24502614
/// [slices]: ../../std/primitive.slice.html

0 commit comments

Comments
 (0)