Skip to content

Commit bda1416

Browse files
hkBstgitbot
authored and
gitbot
committed
Improve prose around into_slice example of IterMut
1 parent 730d7b3 commit bda1416

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
@@ -247,28 +247,21 @@ impl<'a, T> IterMut<'a, T> {
247247
/// Basic usage:
248248
///
249249
/// ```
250-
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
251-
/// // struct (`&[usize]` here):
250+
/// // First, we need a slice to call the `iter_mut` method on:
252251
/// let mut slice = &mut [1, 2, 3];
253252
///
254-
/// {
255-
/// // Then, we get the iterator:
256-
/// let mut iter = slice.iter_mut();
257-
/// // We move to next element:
258-
/// iter.next();
259-
/// // So if we print what `into_slice` method returns here, we have "[2, 3]":
260-
/// println!("{:?}", iter.into_slice());
261-
/// }
262-
///
263-
/// // Now let's modify a value of the slice:
264-
/// {
265-
/// // First we get back the iterator:
266-
/// let mut iter = slice.iter_mut();
267-
/// // We change the value of the first element of the slice returned by the `next` method:
268-
/// *iter.next().unwrap() += 1;
269-
/// }
270-
/// // Now slice is "[2, 2, 3]":
271-
/// println!("{slice:?}");
253+
/// // Then we call `iter_mut` on the slice to get the `IterMut` struct:
254+
/// let mut iter = slice.iter_mut();
255+
/// // Now, we call the `next` method to remove the first element of the iterator,
256+
/// // unwrap and dereference what we get from `next` and increase its value by 1:
257+
/// *iter.next().unwrap() += 1;
258+
/// // Here the iterator does not contain the first element of the slice any more,
259+
/// // so `into_slice` only returns the last two elements of the slice,
260+
/// // and so this prints "[2, 3]":
261+
/// println!("{:?}", iter.into_slice());
262+
/// // The underlying slice still contains three elements, but its first element
263+
/// // was increased by 1, so this prints "[2, 2, 3]":
264+
/// println!("{:?}", slice);
272265
/// ```
273266
#[must_use = "`self` will be dropped if the result is not used"]
274267
#[stable(feature = "iter_to_slice", since = "1.4.0")]

0 commit comments

Comments
 (0)