Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add try_remove to Vec
This provides a 'non-panicky' way of removing an element from a vector
When the index exists, we return it; Otherwise, `None` is returned
  • Loading branch information
ohsayan committed Oct 3, 2020
commit 1e27599bf5d7e300ff6181049a3e771f6a4825b9
33 changes: 33 additions & 0 deletions library/alloc/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,39 @@ impl<T> Vec<T> {
}
}
Comment thread
ohsayan marked this conversation as resolved.

/// Tries to remove an element from the vector. If the element at `index` does not exist,
/// `None` is returned. Otherwise `Some(T)` is returned
///
/// # Examples
/// ```
/// let mut v = vec![1, 2, 3];
/// assert_eq!(v.remove(0), Some(1));
/// assert_eq!(v.remove(2), None);
/// ```
pub fn try_remove(&mut self, index: usize) -> Option<T> {
Comment thread
ohsayan marked this conversation as resolved.
let len = self.len();
if index >= len {
None
} else {
unsafe {
// infallible
let ret;
{
// the place we are taking from.
let ptr = self.as_mut_ptr().add(index);
// copy it out, unsafely having a copy of the value on
// the stack and in the vector at the same time.
ret = ptr::read(ptr);

// Shift everything down to fill in that spot.
ptr::copy(ptr.offset(1), ptr, len - index - 1);
}
self.set_len(len - 1);
Some(ret)
}
}
}

/// Retains only the elements specified by the predicate.
///
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
Expand Down