@@ -49,7 +49,7 @@ impl<'a, T> IntoIterator for &'a mut [T] {
49
49
/// // First, we need a slice to call the `iter` method on:
50
50
/// let slice = &[1, 2, 3];
51
51
///
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 ,
53
53
/// // and iterate over it:
54
54
/// for element in slice.iter() {
55
55
/// println!("{element}");
@@ -107,24 +107,20 @@ impl<'a, T> Iter<'a, T> {
107
107
108
108
/// Views the underlying data as a subslice of the original data.
109
109
///
110
- /// This has the same lifetime as the original slice, and so the
111
- /// iterator can continue to be used while this exists.
112
- ///
113
110
/// # Examples
114
111
///
115
112
/// Basic usage:
116
113
///
117
114
/// ```
118
115
/// // First, we need a slice to call the `iter` method on:
119
- /// // struct (`&[usize]` here):
120
116
/// let slice = &[1, 2, 3];
121
117
///
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 :
123
119
/// let mut iter = slice.iter();
124
120
/// // Here `as_slice` still returns the whole slice, so this prints "[1, 2, 3]":
125
121
/// println!("{:?}", iter.as_slice());
126
122
///
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:
128
124
/// iter.next();
129
125
/// // Here the iterator does not contain the first element of the slice any more,
130
126
/// // so `as_slice` only returns the last two elements of the slice,
@@ -181,7 +177,7 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
181
177
/// // First, we need a slice to call the `iter_mut` method on:
182
178
/// let slice = &mut [1, 2, 3];
183
179
///
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 ,
185
181
/// // iterate over it and increment each element value:
186
182
/// for element in slice.iter_mut() {
187
183
/// *element += 1;
@@ -286,25 +282,30 @@ impl<'a, T> IterMut<'a, T> {
286
282
287
283
/// Views the underlying data as a subslice of the original data.
288
284
///
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
- ///
292
285
/// # Examples
293
286
///
294
287
/// Basic usage:
295
288
///
296
289
/// ```
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];
298
292
///
299
- /// // First, we get the iterator:
293
+ /// // Then we call `iter_mut` on the slice to get the `IterMut` iterator:
300
294
/// 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());
303
297
///
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);
308
309
/// ```
309
310
#[ must_use]
310
311
#[ stable( feature = "slice_iter_mut_as_slice" , since = "1.53.0" ) ]
@@ -315,9 +316,6 @@ impl<'a, T> IterMut<'a, T> {
315
316
316
317
/// Views the underlying data as a mutable subslice of the original data.
317
318
///
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
- ///
321
319
/// # Examples
322
320
///
323
321
/// Basic usage:
0 commit comments