Skip to content

Commit 1a3c46f

Browse files
authored
Rollup merge of rust-lang#35019 - frewsxcv:slice-split, r=GuillaumeGomez
Rewrite/expansion of `slice::split` doc examples. None
2 parents 8de36f1 + a139772 commit 1a3c46f

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/libcollections/slice.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -691,15 +691,40 @@ impl<T> [T] {
691691
///
692692
/// # Examples
693693
///
694-
/// Print the slice split by numbers divisible by 3 (i.e. `[10, 40]`,
695-
/// `[20]`, `[50]`):
694+
/// ```
695+
/// let slice = [10, 40, 33, 20];
696+
/// let mut iter = slice.split(|num| num % 3 == 0);
696697
///
698+
/// assert_eq!(iter.next().unwrap(), &[10, 40]);
699+
/// assert_eq!(iter.next().unwrap(), &[20]);
700+
/// assert!(iter.next().is_none());
697701
/// ```
698-
/// let v = [10, 40, 30, 20, 60, 50];
699702
///
700-
/// for group in v.split(|num| *num % 3 == 0) {
701-
/// println!("{:?}", group);
702-
/// }
703+
/// If the first element is matched, an empty slice will be the first item
704+
/// returned by the iterator. Similarly, if the last element in the slice
705+
/// is matched, an empty slice will be the last item returned by the
706+
/// iterator:
707+
///
708+
/// ```
709+
/// let slice = [10, 40, 33];
710+
/// let mut iter = slice.split(|num| num % 3 == 0);
711+
///
712+
/// assert_eq!(iter.next().unwrap(), &[10, 40]);
713+
/// assert_eq!(iter.next().unwrap(), &[]);
714+
/// assert!(iter.next().is_none());
715+
/// ```
716+
///
717+
/// If two matched elements are directly adjacent, an empty slice will be
718+
/// present between them:
719+
///
720+
/// ```
721+
/// let slice = [10, 6, 33, 20];
722+
/// let mut iter = slice.split(|num| num % 3 == 0);
723+
///
724+
/// assert_eq!(iter.next().unwrap(), &[10]);
725+
/// assert_eq!(iter.next().unwrap(), &[]);
726+
/// assert_eq!(iter.next().unwrap(), &[20]);
727+
/// assert!(iter.next().is_none());
703728
/// ```
704729
#[stable(feature = "rust1", since = "1.0.0")]
705730
#[inline]

0 commit comments

Comments
 (0)