pub struct NonEmptyMutSlice<'a, T: Sized> { /* private fields */ }
Expand description
A non-empty mutable slice type, counterpart of &mut [T]
.
Implementations§
Source§impl<'a, T: Sized> NonEmptyMutSlice<'a, T>
impl<'a, T: Sized> NonEmptyMutSlice<'a, T>
Sourcepub fn from_slice(slice: &'a mut [T]) -> Self
pub fn from_slice(slice: &'a mut [T]) -> Self
Converts a &[T]
into a NonEmptyMutSlice
.
§Panics
This function will panic if the passed slice is empty.
Sourcepub fn from_slice_checked(slice: &'a mut [T]) -> Option<Self>
pub fn from_slice_checked(slice: &'a mut [T]) -> Option<Self>
Converts a &[T]
into a NonEmptyMutSlice
.
Returns None
if the passed slice is empty.
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Returns an unsafe mutable pointer to the slice’s buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage.
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice from this type.
Sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the number of elements in the slice.
Sourcepub fn first(&self) -> &T
pub fn first(&self) -> &T
Returns the first element of the slice.
let arr = &mut [10, 40, 30];
let s = NonEmptyMutSlice::from_slice(arr);
assert_eq!(s.first(), &10);
Sourcepub fn first_mut(&mut self) -> &mut T
pub fn first_mut(&mut self) -> &mut T
Returns a mutable pointer to the first element of the slice.
let arr = &mut [10, 40, 30];
let mut s = NonEmptyMutSlice::from_slice(arr);
*s.first_mut() = 42;
assert_eq!(arr, &[42, 40, 30]);
Sourcepub fn last(&self) -> &T
pub fn last(&self) -> &T
Returns the last element of the slice.
let arr = &mut [10, 40, 30];
let s = NonEmptyMutSlice::from_slice(arr);
assert_eq!(s.last(), &30);
Sourcepub fn last_mut(&mut self) -> &mut T
pub fn last_mut(&mut self) -> &mut T
Returns the last element of the slice.
let arr = &mut [10, 40, 30];
let mut s = NonEmptyMutSlice::from_slice(arr);
*s.last_mut() = 42;
assert_eq!(arr, &[10, 40, 42]);
Sourcepub fn split_first(&self) -> (&T, &[T])
pub fn split_first(&self) -> (&T, &[T])
Returns the first and all the rest of the elements of the slice.
let arr = &mut [10, 40, 30];
let s = NonEmptyMutSlice::from_slice(arr);
assert_eq!(s.split_first(), (&10, &[40, 30][..]));
Sourcepub fn split_first_mut(&mut self) -> (&mut T, &mut [T])
pub fn split_first_mut(&mut self) -> (&mut T, &mut [T])
Returns the first and all the rest of the elements of the slice.
let arr = &mut [0, 1, 2];
let mut s = NonEmptyMutSlice::from_slice(arr);
let (first, rest) = s.split_first_mut();
*first = 3;
rest[0] = 4;
rest[1] = 5;
assert_eq!(arr, &[3, 4, 5]);
Sourcepub fn split_last(&self) -> (&T, &[T])
pub fn split_last(&self) -> (&T, &[T])
Returns the last and all the rest of the elements of the slice.
let arr = &mut [10, 40, 30];
let s = NonEmptyMutSlice::from_slice(arr);
assert_eq!(s.split_last(), (&30, &[10, 40][..]));
Sourcepub fn split_last_mut(&mut self) -> (&mut T, &mut [T])
pub fn split_last_mut(&mut self) -> (&mut T, &mut [T])
Returns the last and all the rest of the elements of the slice.
let arr = &mut [0, 1, 2];
let mut s = NonEmptyMutSlice::from_slice(arr);
let (last, rest) = s.split_last_mut();
*last = 3;
rest[0] = 4;
rest[1] = 5;
assert_eq!(arr, &[4, 5, 3]);