Skip to content

Commit d38b5d4

Browse files
hkBstgitbot
authored and
gitbot
committed
Improve prose around into_slice example of IterMut
1 parent 0c4799d commit d38b5d4

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

core/src/slice/iter.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -259,28 +259,21 @@ impl<'a, T> IterMut<'a, T> {
259259
/// Basic usage:
260260
///
261261
/// ```
262-
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
263-
/// // struct (`&[usize]` here):
262+
/// // First, we need a slice to call the `iter_mut` method on:
264263
/// let mut slice = &mut [1, 2, 3];
265264
///
266-
/// {
267-
/// // Then, we get the iterator:
268-
/// let mut iter = slice.iter_mut();
269-
/// // We move to next element:
270-
/// iter.next();
271-
/// // So if we print what `into_slice` method returns here, we have "[2, 3]":
272-
/// println!("{:?}", iter.into_slice());
273-
/// }
274-
///
275-
/// // Now let's modify a value of the slice:
276-
/// {
277-
/// // First we get back the iterator:
278-
/// let mut iter = slice.iter_mut();
279-
/// // We change the value of the first element of the slice returned by the `next` method:
280-
/// *iter.next().unwrap() += 1;
281-
/// }
282-
/// // Now slice is "[2, 2, 3]":
283-
/// println!("{slice:?}");
265+
/// // Then we call `iter_mut` on the slice to get the `IterMut` struct:
266+
/// let mut iter = slice.iter_mut();
267+
/// // Now, we call the `next` method to remove the first element of the iterator,
268+
/// // unwrap and dereference what we get from `next` and increase its value by 1:
269+
/// *iter.next().unwrap() += 1;
270+
/// // Here the iterator does not contain the first element of the slice any more,
271+
/// // so `into_slice` only returns the last two elements of the slice,
272+
/// // and so this prints "[2, 3]":
273+
/// println!("{:?}", iter.into_slice());
274+
/// // The underlying slice still contains three elements, but its first element
275+
/// // was increased by 1, so this prints "[2, 2, 3]":
276+
/// println!("{:?}", slice);
284277
/// ```
285278
#[must_use = "`self` will be dropped if the result is not used"]
286279
#[stable(feature = "iter_to_slice", since = "1.4.0")]

0 commit comments

Comments
 (0)