/// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec`
/// and then filling it back up to the same [`len`] should incur no calls to
/// the allocator. If you wish to free up unused memory, use
-/// [`shrink_to_fit`][`shrink_to_fit`].
+/// [`shrink_to_fit`].
///
/// [`push`] and [`insert`] will never (re)allocate if the reported capacity is
/// sufficient. [`push`] and [`insert`] *will* (re)allocate if
///
/// ```
/// let mut vec = vec![1, 2, 3, 4];
- /// vec.retain(|&x| x%2 == 0);
+ /// vec.retain(|&x| x % 2 == 0);
/// assert_eq!(vec, [2, 4]);
/// ```
///
pub fn dedup(&mut self) {
self.dedup_by(|a, b| a == b)
}
+}
+impl<T> Vec<T> {
/// Removes the first instance of `item` from the vector if the item exists.
///
/// # Examples
/// assert_eq!(vec, vec![2, 3, 1]);
/// ```
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
- pub fn remove_item(&mut self, item: &T) -> Option<T> {
+ pub fn remove_item<V>(&mut self, item: &V) -> Option<T>
+ where
+ T: PartialEq<V>,
+ {
let pos = self.iter().position(|x| *x == *item)?;
Some(self.remove(pos))
}
/// An iterator that moves out of a vector.
///
-/// This `struct` is created by the `into_iter` method on [`Vec`][`Vec`] (provided
+/// This `struct` is created by the `into_iter` method on [`Vec`] (provided
/// by the [`IntoIterator`] trait).
///
/// [`Vec`]: struct.Vec.html