about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarijn Schouten <hkBst@users.noreply.github.com>2024-12-20 19:57:20 +0100
committerGitHub <noreply@github.com>2024-12-20 19:57:20 +0100
commit07ab203f340138c6e2df5f32a3a5eb97b1dd90ce (patch)
tree56ca3c753d7c9b5a8a3e62fc737a36f7f4f47df4
parentfcc1615e47100b376d0a6166faccdd4a8253c314 (diff)
downloadrust-07ab203f340138c6e2df5f32a3a5eb97b1dd90ce.tar.gz
rust-07ab203f340138c6e2df5f32a3a5eb97b1dd90ce.zip
Improve prose around into_slice example of IterMut
-rw-r--r--library/core/src/slice/iter.rs33
1 files changed, 13 insertions, 20 deletions
diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs
index c5746157d01..5e22ac34013 100644
--- a/library/core/src/slice/iter.rs
+++ b/library/core/src/slice/iter.rs
@@ -247,28 +247,21 @@ impl<'a, T> IterMut<'a, T> {
     /// Basic usage:
     ///
     /// ```
-    /// // First, we declare a type which has `iter_mut` method to get the `IterMut`
-    /// // struct (`&[usize]` here):
+    /// // First, we need a slice to call the `iter_mut` method on:
     /// let mut slice = &mut [1, 2, 3];
     ///
-    /// {
-    ///     // Then, we get the iterator:
-    ///     let mut iter = slice.iter_mut();
-    ///     // We move to next element:
-    ///     iter.next();
-    ///     // So if we print what `into_slice` method returns here, we have "[2, 3]":
-    ///     println!("{:?}", iter.into_slice());
-    /// }
-    ///
-    /// // Now let's modify a value of the slice:
-    /// {
-    ///     // First we get back the iterator:
-    ///     let mut iter = slice.iter_mut();
-    ///     // We change the value of the first element of the slice returned by the `next` method:
-    ///     *iter.next().unwrap() += 1;
-    /// }
-    /// // Now slice is "[2, 2, 3]":
-    /// println!("{slice:?}");
+    /// // Then we call `iter_mut` on the slice to get the `IterMut` struct:
+    /// let mut iter = slice.iter_mut();
+    /// // Now, we call the `next` method to remove the first element of the iterator,
+    /// // unwrap and dereference what we get from `next` and increase its value by 1:
+    /// *iter.next().unwrap() += 1;
+    /// // Here the iterator does not contain the first element of the slice any more,
+    /// // so `into_slice` only returns the last two elements of the slice,
+    /// // and so this prints "[2, 3]":
+    /// println!("{:?}", iter.into_slice());
+    /// // The underlying slice still contains three elements, but its first element
+    /// // was increased by 1, so this prints "[2, 2, 3]":
+    /// println!("{:?}", slice);
     /// ```
     #[must_use = "`self` will be dropped if the result is not used"]
     #[stable(feature = "iter_to_slice", since = "1.4.0")]