Rollup merge of #127668 - spencer3035:improve-slice-doc, r=jhpratt
Improved slice documentation Improve slice documentation to include assert_eq checks for all the cases where there were existing examples. I think it makes things more clear when the documentation explicitly checks against values and shows the reader what it does. I also started a rust internals discussion about it here: https://internals.rust-lang.org/t/improve-slice-documentaion/21168
This commit is contained in:
commit
f0119130de
@ -388,6 +388,9 @@ pub(super) trait SplitIter: DoubleEndedIterator {
|
|||||||
/// ```
|
/// ```
|
||||||
/// let slice = [10, 40, 33, 20];
|
/// let slice = [10, 40, 33, 20];
|
||||||
/// let mut iter = slice.split(|num| num % 3 == 0);
|
/// let mut iter = slice.split(|num| num % 3 == 0);
|
||||||
|
/// assert_eq!(iter.next(), Some(&[10, 40][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&[20][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`split`]: slice::split
|
/// [`split`]: slice::split
|
||||||
@ -541,6 +544,9 @@ impl<T, P> FusedIterator for Split<'_, T, P> where P: FnMut(&T) -> bool {}
|
|||||||
/// ```
|
/// ```
|
||||||
/// let slice = [10, 40, 33, 20];
|
/// let slice = [10, 40, 33, 20];
|
||||||
/// let mut iter = slice.split_inclusive(|num| num % 3 == 0);
|
/// let mut iter = slice.split_inclusive(|num| num % 3 == 0);
|
||||||
|
/// assert_eq!(iter.next(), Some(&[10, 40, 33][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&[20][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`split_inclusive`]: slice::split_inclusive
|
/// [`split_inclusive`]: slice::split_inclusive
|
||||||
@ -914,7 +920,10 @@ impl<T, P> FusedIterator for SplitInclusiveMut<'_, T, P> where P: FnMut(&T) -> b
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = [11, 22, 33, 0, 44, 55];
|
/// let slice = [11, 22, 33, 0, 44, 55];
|
||||||
/// let iter = slice.rsplit(|num| *num == 0);
|
/// let mut iter = slice.rsplit(|num| *num == 0);
|
||||||
|
/// assert_eq!(iter.next(), Some(&[44, 55][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&[11, 22, 33][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`rsplit`]: slice::rsplit
|
/// [`rsplit`]: slice::rsplit
|
||||||
@ -1134,7 +1143,10 @@ fn size_hint(&self) -> (usize, Option<usize>) {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = [10, 40, 30, 20, 60, 50];
|
/// let slice = [10, 40, 30, 20, 60, 50];
|
||||||
/// let iter = slice.splitn(2, |num| *num % 3 == 0);
|
/// let mut iter = slice.splitn(2, |num| *num % 3 == 0);
|
||||||
|
/// assert_eq!(iter.next(), Some(&[10, 40][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&[20, 60, 50][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`splitn`]: slice::splitn
|
/// [`splitn`]: slice::splitn
|
||||||
@ -1175,7 +1187,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = [10, 40, 30, 20, 60, 50];
|
/// let slice = [10, 40, 30, 20, 60, 50];
|
||||||
/// let iter = slice.rsplitn(2, |num| *num % 3 == 0);
|
/// let mut iter = slice.rsplitn(2, |num| *num % 3 == 0);
|
||||||
|
/// assert_eq!(iter.next(), Some(&[50][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&[10, 40, 30, 20][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`rsplitn`]: slice::rsplitn
|
/// [`rsplitn`]: slice::rsplitn
|
||||||
@ -1300,7 +1315,11 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = ['r', 'u', 's', 't'];
|
/// let slice = ['r', 'u', 's', 't'];
|
||||||
/// let iter = slice.windows(2);
|
/// let mut iter = slice.windows(2);
|
||||||
|
/// assert_eq!(iter.next(), Some(&['r', 'u'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['u', 's'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['s', 't'][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`windows`]: slice::windows
|
/// [`windows`]: slice::windows
|
||||||
@ -1448,7 +1467,11 @@ unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Windows<'a, T> {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
||||||
/// let iter = slice.chunks(2);
|
/// let mut iter = slice.chunks(2);
|
||||||
|
/// assert_eq!(iter.next(), Some(&['l', 'o'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['r', 'e'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['m'][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`chunks`]: slice::chunks
|
/// [`chunks`]: slice::chunks
|
||||||
@ -1819,7 +1842,10 @@ unsafe impl<T> Sync for ChunksMut<'_, T> where T: Sync {}
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
||||||
/// let iter = slice.chunks_exact(2);
|
/// let mut iter = slice.chunks_exact(2);
|
||||||
|
/// assert_eq!(iter.next(), Some(&['l', 'o'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['r', 'e'][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`chunks_exact`]: slice::chunks_exact
|
/// [`chunks_exact`]: slice::chunks_exact
|
||||||
@ -2163,7 +2189,11 @@ unsafe impl<T> Sync for ChunksExactMut<'_, T> where T: Sync {}
|
|||||||
/// #![feature(array_windows)]
|
/// #![feature(array_windows)]
|
||||||
///
|
///
|
||||||
/// let slice = [0, 1, 2, 3];
|
/// let slice = [0, 1, 2, 3];
|
||||||
/// let iter = slice.array_windows::<2>();
|
/// let mut iter = slice.array_windows::<2>();
|
||||||
|
/// assert_eq!(iter.next(), Some(&[0, 1]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&[1, 2]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&[2, 3]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`array_windows`]: slice::array_windows
|
/// [`array_windows`]: slice::array_windows
|
||||||
@ -2285,7 +2315,10 @@ fn is_empty(&self) -> bool {
|
|||||||
/// #![feature(array_chunks)]
|
/// #![feature(array_chunks)]
|
||||||
///
|
///
|
||||||
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
||||||
/// let iter = slice.array_chunks::<2>();
|
/// let mut iter = slice.array_chunks::<2>();
|
||||||
|
/// assert_eq!(iter.next(), Some(&['l', 'o']));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['r', 'e']));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`array_chunks`]: slice::array_chunks
|
/// [`array_chunks`]: slice::array_chunks
|
||||||
@ -2526,7 +2559,11 @@ unsafe impl<'a, T, const N: usize> TrustedRandomAccessNoCoerce for ArrayChunksMu
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
||||||
/// let iter = slice.rchunks(2);
|
/// let mut iter = slice.rchunks(2);
|
||||||
|
/// assert_eq!(iter.next(), Some(&['e', 'm'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['o', 'r'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['l'][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`rchunks`]: slice::rchunks
|
/// [`rchunks`]: slice::rchunks
|
||||||
@ -2892,7 +2929,10 @@ unsafe impl<T> Sync for RChunksMut<'_, T> where T: Sync {}
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
/// let slice = ['l', 'o', 'r', 'e', 'm'];
|
||||||
/// let iter = slice.rchunks_exact(2);
|
/// let mut iter = slice.rchunks_exact(2);
|
||||||
|
/// assert_eq!(iter.next(), Some(&['e', 'm'][..]));
|
||||||
|
/// assert_eq!(iter.next(), Some(&['o', 'r'][..]));
|
||||||
|
/// assert_eq!(iter.next(), None);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`rchunks_exact`]: slice::rchunks_exact
|
/// [`rchunks_exact`]: slice::rchunks_exact
|
||||||
|
Loading…
Reference in New Issue
Block a user