diff options
| author | Corey Farwell <coreyf@rwell.org> | 2016-07-22 20:23:25 -0400 |
|---|---|---|
| committer | Corey Farwell <coreyf@rwell.org> | 2016-07-23 11:59:31 -0400 |
| commit | c77f8ce7c3284441a00faed6782d08eb5a78296c (patch) | |
| tree | 5734cc675b844cceed4ce2c318ce7b04d56418ce | |
| parent | e7c822cee29b5b939340c2cb0dfefa9a49742d77 (diff) | |
| download | rust-c77f8ce7c3284441a00faed6782d08eb5a78296c.tar.gz rust-c77f8ce7c3284441a00faed6782d08eb5a78296c.zip | |
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`
| -rw-r--r-- | src/libcollections/slice.rs | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 2c54dc13c8d..1f8eea56fc6 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -544,14 +544,21 @@ impl<T> [T] { /// /// # Example /// - /// Print the adjacent pairs of a slice (i.e. `[1,2]`, `[2,3]`, - /// `[3,4]`): + /// ``` + /// let slice = ['r', 'u', 's', 't']; + /// let mut iter = slice.windows(2); + /// assert_eq!(iter.next().unwrap(), &['r', 'u']); + /// assert_eq!(iter.next().unwrap(), &['u', 's']); + /// assert_eq!(iter.next().unwrap(), &['s', 't']); + /// assert!(iter.next().is_none()); + /// ``` /// - /// ```rust - /// let v = &[1, 2, 3, 4]; - /// for win in v.windows(2) { - /// println!("{:?}", win); - /// } + /// If the slice is shorter than `size`: + /// + /// ``` + /// let slice = ['f', 'o', 'o']; + /// let mut iter = slice.windows(4); + /// assert!(iter.next().is_none()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] |
