Struct IterMut

#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct IterMut<'a, T: 'a> { pub(in ::slice::iter) ptr: NonNull<T>, pub(in ::slice::iter) end_or_len: *mut T, pub(in ::slice::iter) _marker: PhantomData<&'a mut T> }

Mutable slice iterator.

This struct is created by the iter_mut method on slices.

Examples

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:?}");

Fields

ptr: NonNull<T>

The pointer to the next element to return, or the past-the-end location if the iterator is empty.

This address will be used for all ZST elements, never changed.

end_or_len: *mut T

For non-ZSTs, the non-null pointer to the past-the-end element.

For ZSTs, this is ptr::without_provenance_mut(len).

_marker: PhantomData<&'a mut T>

Implementations

impl<'a, T> IterMut<'a, T>

const fn new(slice: &'a mut [T]) -> Self
fn into_slice(self) -> &'a mut [T]

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.

Examples

Basic usage:

// 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);
fn as_slice(&self) -> &[T]

Views the underlying data as a subslice of the original data.

Examples

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:
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);
fn as_mut_slice(&mut self) -> &mut [T]

Views the underlying data as a mutable subslice of the original data.

Examples

Basic usage:

#![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]);

impl<'a, T> IterMut<'a, T>

unsafe fn next_back_unchecked(&mut self) -> &'a mut T

Returns the last element and moves the end of the iterator backwards by 1.

Safety

The iterator must not be empty

fn make_slice(&self) -> &'a [T]
unsafe fn post_inc_start(&mut self, offset: usize) -> NonNull<T>
unsafe fn pre_dec_end(&mut self, offset: usize) -> NonNull<T>

Trait Implementations

impl<'a, T> DoubleEndedIterator for IterMut<'a, T>

fn next_back(&mut self) -> Option<&'a mut T>
fn nth_back(&mut self, n: usize) -> Option<&'a mut T>
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>>

impl<'a, T> Iterator for IterMut<'a, T>

type Item = &'a mut T;
fn next(&mut self) -> Option<&'a mut T>
fn next_chunk<const N: usize>(&mut self) -> Result<[&'a mut T; N], IntoIter<&'a mut T, N>>
fn size_hint(&self) -> (usize, Option<usize>)
fn count(self) -> usize
fn nth(&mut self, n: usize) -> Option<&'a mut T>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn last(self) -> Option<&'a mut T>
fn fold<B, F>(self, init: B, f: F) -> B
where
    F: FnMut(B, Self::Item) -> B,
fn for_each<F>(self, f: F)
where
    Self: Sized,
    F: FnMut(Self::Item),
fn all<F>(&mut self, f: F) -> bool
where
    Self: Sized,
    F: FnMut(Self::Item) -> bool,
fn any<F>(&mut self, f: F) -> bool
where
    Self: Sized,
    F: FnMut(Self::Item) -> bool,
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
    Self: Sized,
    P: FnMut(&Self::Item) -> bool,
fn find_map<B, F>(&mut self, f: F) -> Option<B>
where
    Self: Sized,
    F: FnMut(Self::Item) -> Option<B>,
fn position<P>(&mut self, predicate: P) -> Option<usize>
where
    Self: Sized,
    P: FnMut(Self::Item) -> bool,
fn rposition<P>(&mut self, predicate: P) -> Option<usize>
where
    P: FnMut(Self::Item) -> bool,
    Self: Sized + ExactSizeIterator + DoubleEndedIterator,
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item

impl<'a, T> TrustedRandomAccess for IterMut<'a, T>

impl<'a, T> TrustedRandomAccessNoCoerce for IterMut<'a, T>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl<T> AsRef<[T]> for IterMut<'_, T>

fn as_ref(&self) -> &[T]

impl<T> Default for IterMut<'_, T>

fn default() -> Self

Creates an empty slice iterator.

# use core::slice::IterMut;
let iter: IterMut < '_, u8 > = Default::default();
assert_eq!(iter.len(), 0);

impl<T> ExactSizeIterator for IterMut<'_, T>

fn len(&self) -> usize
fn is_empty(&self) -> bool

impl<T> FusedIterator for IterMut<'_, T>

impl<T> TrustedLen for IterMut<'_, T>

impl<T: Debug> Debug for IterMut<'_, T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: Send> Send for IterMut<'_, T>

impl<T: Sync> Sync for IterMut<'_, T>

Auto Trait Implementations

impl<'a, T> !UnwindSafe for IterMut<'a, T>

impl<'a, T> Freeze for IterMut<'a, T> where NonNull<T>: Freeze, *mut T: Freeze, PhantomData<&'a mut T>: Freeze,

impl<'a, T> RefUnwindSafe for IterMut<'a, T> where NonNull<T>: RefUnwindSafe, *mut T: RefUnwindSafe, PhantomData<&'a mut T>: RefUnwindSafe,

impl<'a, T> Unpin for IterMut<'a, T> where NonNull<T>: Unpin, *mut T: Unpin, PhantomData<&'a mut T>: Unpin,

impl<'a, T> UnsafeUnpin for IterMut<'a, T> where NonNull<T>: UnsafeUnpin, *mut T: UnsafeUnpin, PhantomData<&'a mut T>: UnsafeUnpin,

Blanket Implementations

impl<I> IntoIterator for IterMut<'a, T> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for IterMut<'a, T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for IterMut<'a, T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for IterMut<'a, T> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for IterMut<'a, T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for IterMut<'a, T> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for IterMut<'a, T>

impl<T, U> Into<U> for IterMut<'a, T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for IterMut<'a, T> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for IterMut<'a, T> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>