1.0.0[][src]Struct alloc::slice::IterMut

pub struct IterMut<'a, T> where
    T: 'a, 
{ /* fields omitted */ }

Mutable slice iterator.

This struct is created by the iter_mut method on slices.

Examples

Basic usage:

// First, we declare a type which has `iter_mut` method to get the `IterMut`
// struct (&[usize here]):
let mut slice = &mut [1, 2, 3];

// Then, we iterate over it and increment each element value:
for element in slice.iter_mut() {
    *element += 1;
}

// We now have "[2, 3, 4]":
println!("{:?}", slice);

Methods

impl<'a, T> IterMut<'a, T>[src]

pub fn into_slice(self) -> &'a mut [T]1.4.0[src]

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 declare a type which has `iter_mut` method to get the `IterMut`
// struct (&[usize here]):
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);

pub fn as_slice(&self) -> &[T][src]

🔬 This is a nightly-only experimental API. (slice_iter_mut_as_slice #58957)

recently added

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

To avoid creating &mut [T] references that alias, the returned slice borrows its lifetime from the iterator the method is applied on.

Examples

Basic usage:

let mut slice: &mut [usize] = &mut [1, 2, 3];

// First, we get the iterator:
let mut iter = slice.iter_mut();
// So if we check what the `as_slice` method returns here, we have "[1, 2, 3]":
assert_eq!(iter.as_slice(), &[1, 2, 3]);

// Next, we move to the second element of the slice:
iter.next();
// Now `as_slice` returns "[2, 3]":
assert_eq!(iter.as_slice(), &[2, 3]);

Trait Implementations

impl<'a, T> DoubleEndedIterator for IterMut<'a, T>[src]

impl<'_, T> Debug for IterMut<'_, T> where
    T: Debug
1.9.0[src]

impl<'_, T> Sync for IterMut<'_, T> where
    T: Sync
[src]

impl<'_, T> FusedIterator for IterMut<'_, T>1.26.0[src]

impl<'_, T> Send for IterMut<'_, T> where
    T: Send
[src]

impl<'_, T> ExactSizeIterator for IterMut<'_, T>[src]

impl<'a, T> Iterator for IterMut<'a, T>[src]

type Item = &'a mut T

The type of the elements being iterated over.

impl<'_, T> TrustedLen for IterMut<'_, T>[src]

Auto Trait Implementations

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

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]