pub struct IterMut<'a, T>where T: 'a,{ /* private fields */ }
Mutable slice iterator.
This struct is created by the iter_mut method on slices.
iter_mut
Basic usage:
/ First, we need a slice to call the `iter_mut` method on: let slice = &mut [1, 2, 3]; / Then we call `iter_mut` on the slice to get the `IterMut` iterator, / iterate over it and increment each element value: for element in slice.iter_mut() { *element += 1; } / We now have "[2, 3, 4]": println!("{slice:?}");
Views the underlying data as a subslice of the original data.
To avoid creating &mut references that alias, this is forced to consume the iterator.
&mut
/ First, we need a slice to call the `iter_mut` method on: let mut slice = &mut [1, 2, 3]; / 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);
/ First, we need a slice to call the `iter_mut` method on: let slice = &mut [1, 2, 3]; / Then we call `iter_mut` on the slice to get the `IterMut` iterator: let mut iter = slice.iter_mut(); / Here `as_slice` still returns the whole slice, so this prints "[1, 2, 3]": println!("{:?}", iter.as_slice()); / Now, we call the `next` method to remove the first element from the iterator / and increment its value: *iter.next().unwrap() += 1; / Here the iterator does not contain the first element of the slice any more, / so `as_slice` only returns the last two elements of the slice, / and so this prints "[2, 3]": println!("{:?}", iter.as_slice()); / The underlying slice still contains three elements, but its first element / was increased by 1, so this prints "[2, 2, 3]": println!("{:?}", slice);
Views the underlying data as a mutable subslice of the original data.
#![feature(slice_iter_mut_as_mut_slice)] let mut slice: &mut [usize] = &mut [1, 2, 3]; / First, we get the iterator: let mut iter = slice.iter_mut(); / Then, we get a mutable slice from it: let mut_slice = iter.as_mut_slice(); / So if we check what the `as_mut_slice` method returned, we have "[1, 2, 3]": assert_eq!(mut_slice, &mut [1, 2, 3]); / We can use it to mutate the slice: mut_slice[0] = 4; mut_slice[2] = 5; / Next, we can move to the second element of the slice, checking that / it yields the value we just wrote: assert_eq!(iter.next(), Some(&mut 4)); / Now `as_mut_slice` returns "[2, 5]": assert_eq!(iter.as_mut_slice(), &mut [2, 5]);
Creates an empty slice iterator.
let iter: IterMut < '_, u8 > = Default::default(); assert_eq!(iter.len(), 0);
n
Iterator::try_fold()
exact_size_is_empty
iter_next_chunk
N
iter_intersperse
separator
peek
peek_mut
skip
fold
iter_map_windows
f
self
slice::windows()
None
Iterator
iterator_try_collect
iter_collect_into
iter_partition_in_place
true
false
iter_is_partitioned
iterator_try_reduce
try_find
clone
iter_array_chunks
iter_order_by
PartialOrd
TypeId
Returns the argument unchanged.
Calls U::from(self).
U::from(self)
That is, this conversion is whatever the implementation of From<T> for U chooses to do.
From<T> for U