Trait generic_array::sequence::Remove
source · pub unsafe trait Remove<T, N: ArrayLength>: GenericSequence<T> {
type Output: GenericSequence<T>;
// Required methods
unsafe fn remove_unchecked(self, idx: usize) -> (T, Self::Output);
unsafe fn swap_remove_unchecked(self, idx: usize) -> (T, Self::Output);
// Provided methods
fn remove(self, idx: usize) -> (T, Self::Output) { ... }
fn swap_remove(self, idx: usize) -> (T, Self::Output) { ... }
}
Expand description
Defines a GenericSequence
which can be shortened by removing an element at a given index.
§Safety
While the remove
and swap_remove
methods are marked safe,
care must be taken when implementing it. The remove_unchecked
and swap_remove_unchecked
methods are unsafe
and must be used with caution.
Required Associated Types§
sourcetype Output: GenericSequence<T>
type Output: GenericSequence<T>
Resulting sequence formed by removing an element at the given index.
Required Methods§
sourceunsafe fn remove_unchecked(self, idx: usize) -> (T, Self::Output)
unsafe fn remove_unchecked(self, idx: usize) -> (T, Self::Output)
Removes an element at the given index without bounds checking,
shifting elements after the given index to the left to fill the gap,
resulting in a time complexity of O(n) where n=N-idx-1
See remove
for an example.
§Safety
The caller must ensure that the index is within bounds, otherwise it is undefined behavior.
sourceunsafe fn swap_remove_unchecked(self, idx: usize) -> (T, Self::Output)
unsafe fn swap_remove_unchecked(self, idx: usize) -> (T, Self::Output)
Removes an element at the given index without bounds checking, swapping it with the last element.
See swap_remove
for an example.
§Safety
The caller must ensure that the index is within bounds, otherwise it is undefined behavior.
Provided Methods§
sourcefn remove(self, idx: usize) -> (T, Self::Output)
fn remove(self, idx: usize) -> (T, Self::Output)
Removes an element at the given index, shifting elements
after the given index to the left to fill the gap, resulting
in a time complexity of O(n) where n=N-idx-1
§Example
let a = arr![1, 2, 3, 4];
let (removed, b) = a.remove(2);
assert_eq!(removed, 3);
assert_eq!(b, arr![1, 2, 4]);
§Panics
Panics if the index is out of bounds.