Skip to content

Commit 935bf76

Browse files
hkBstgitbot
authored and
gitbot
committed
Improve prose around as_slice example of IterMut
I've removed the cryptic message about not being able to call `&mut self` methods while retaining a shared borrow of the iterator, such as `as_slice` produces. This is just normal borrowing rules and does not seem especially relevant here. I can whip up a replacement if someone thinks it has value.
1 parent fecc5ff commit 935bf76

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

core/src/slice/iter.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a, T> IntoIterator for &'a mut [T] {
4949
/// // First, we need a slice to call the `iter` method on:
5050
/// let slice = &[1, 2, 3];
5151
///
52-
/// // Then we call `iter` on the slice to get the `Iter` struct,
52+
/// // Then we call `iter` on the slice to get the `Iter` iterator,
5353
/// // and iterate over it:
5454
/// for element in slice.iter() {
5555
/// println!("{element}");
@@ -107,24 +107,20 @@ impl<'a, T> Iter<'a, T> {
107107

108108
/// Views the underlying data as a subslice of the original data.
109109
///
110-
/// This has the same lifetime as the original slice, and so the
111-
/// iterator can continue to be used while this exists.
112-
///
113110
/// # Examples
114111
///
115112
/// Basic usage:
116113
///
117114
/// ```
118115
/// // First, we need a slice to call the `iter` method on:
119-
/// // struct (`&[usize]` here):
120116
/// let slice = &[1, 2, 3];
121117
///
122-
/// // Then we call `iter` on the slice to get the `Iter` struct:
118+
/// // Then we call `iter` on the slice to get the `Iter` iterator:
123119
/// let mut iter = slice.iter();
124120
/// // Here `as_slice` still returns the whole slice, so this prints "[1, 2, 3]":
125121
/// println!("{:?}", iter.as_slice());
126122
///
127-
/// // Now, we call the `next` method to remove the first element of the iterator:
123+
/// // Now, we call the `next` method to remove the first element from the iterator:
128124
/// iter.next();
129125
/// // Here the iterator does not contain the first element of the slice any more,
130126
/// // so `as_slice` only returns the last two elements of the slice,
@@ -181,7 +177,7 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
181177
/// // First, we need a slice to call the `iter_mut` method on:
182178
/// let slice = &mut [1, 2, 3];
183179
///
184-
/// // Then we call `iter_mut` on the slice to get the `IterMut` struct,
180+
/// // Then we call `iter_mut` on the slice to get the `IterMut` iterator,
185181
/// // iterate over it and increment each element value:
186182
/// for element in slice.iter_mut() {
187183
/// *element += 1;
@@ -286,25 +282,30 @@ impl<'a, T> IterMut<'a, T> {
286282

287283
/// Views the underlying data as a subslice of the original data.
288284
///
289-
/// To avoid creating `&mut [T]` references that alias, the returned slice
290-
/// borrows its lifetime from the iterator the method is applied on.
291-
///
292285
/// # Examples
293286
///
294287
/// Basic usage:
295288
///
296289
/// ```
297-
/// let mut slice: &mut [usize] = &mut [1, 2, 3];
290+
/// // First, we need a slice to call the `iter_mut` method on:
291+
/// let slice = &mut [1, 2, 3];
298292
///
299-
/// // First, we get the iterator:
293+
/// // Then we call `iter_mut` on the slice to get the `IterMut` iterator:
300294
/// let mut iter = slice.iter_mut();
301-
/// // So if we check what the `as_slice` method returns here, we have "[1, 2, 3]":
302-
/// assert_eq!(iter.as_slice(), &[1, 2, 3]);
295+
/// // Here `as_slice` still returns the whole slice, so this prints "[1, 2, 3]":
296+
/// println!("{:?}", iter.as_slice());
303297
///
304-
/// // Next, we move to the second element of the slice:
305-
/// iter.next();
306-
/// // Now `as_slice` returns "[2, 3]":
307-
/// assert_eq!(iter.as_slice(), &[2, 3]);
298+
/// // Now, we call the `next` method to remove the first element from the iterator
299+
/// // and increment its value:
300+
/// *iter.next().unwrap() += 1;
301+
/// // Here the iterator does not contain the first element of the slice any more,
302+
/// // so `as_slice` only returns the last two elements of the slice,
303+
/// // and so this prints "[2, 3]":
304+
/// println!("{:?}", iter.as_slice());
305+
///
306+
/// // The underlying slice still contains three elements, but its first element
307+
/// // was increased by 1, so this prints "[2, 2, 3]":
308+
/// println!("{:?}", slice);
308309
/// ```
309310
#[must_use]
310311
#[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
@@ -315,9 +316,6 @@ impl<'a, T> IterMut<'a, T> {
315316

316317
/// Views the underlying data as a mutable subslice of the original data.
317318
///
318-
/// To avoid creating `&mut [T]` references that alias, the returned slice
319-
/// borrows its lifetime from the iterator the method is applied on.
320-
///
321319
/// # Examples
322320
///
323321
/// Basic usage:

0 commit comments

Comments
 (0)