Skip to content

Commit c77f8ce

Browse files
committed
Doc example improvements for slice::windows.
* Modify existing example to not rely on printing to see results * Add an example demonstrating when slice is shorter than `size`
1 parent e7c822c commit c77f8ce

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/libcollections/slice.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,21 @@ impl<T> [T] {
544544
///
545545
/// # Example
546546
///
547-
/// Print the adjacent pairs of a slice (i.e. `[1,2]`, `[2,3]`,
548-
/// `[3,4]`):
547+
/// ```
548+
/// let slice = ['r', 'u', 's', 't'];
549+
/// let mut iter = slice.windows(2);
550+
/// assert_eq!(iter.next().unwrap(), &['r', 'u']);
551+
/// assert_eq!(iter.next().unwrap(), &['u', 's']);
552+
/// assert_eq!(iter.next().unwrap(), &['s', 't']);
553+
/// assert!(iter.next().is_none());
554+
/// ```
549555
///
550-
/// ```rust
551-
/// let v = &[1, 2, 3, 4];
552-
/// for win in v.windows(2) {
553-
/// println!("{:?}", win);
554-
/// }
556+
/// If the slice is shorter than `size`:
557+
///
558+
/// ```
559+
/// let slice = ['f', 'o', 'o'];
560+
/// let mut iter = slice.windows(4);
561+
/// assert!(iter.next().is_none());
555562
/// ```
556563
#[stable(feature = "rust1", since = "1.0.0")]
557564
#[inline]

0 commit comments

Comments
 (0)