Merge pull request #75 from niklasf/truncate

Add ArrayVec::truncate()
This commit is contained in:
bluss
2017-10-18 18:35:55 +02:00
committed by GitHub
+19
View File
@@ -442,6 +442,25 @@ impl<A: Array> ArrayVec<A> {
} }
} }
/// Shortens the vector, keeping the first `len` elements and dropping
/// the rest.
///
/// If `len` is greater than the vector's current length this has no
/// effect.
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3, 4, 5]);
/// array.truncate(3);
/// assert_eq!(&array[..], &[1, 2, 3]);
/// array.truncate(4);
/// assert_eq!(&array[..], &[1, 2, 3]);
/// ```
pub fn truncate(&mut self, len: usize) {
while self.len() > len { self.pop(); }
}
/// Remove all elements in the vector. /// Remove all elements in the vector.
pub fn clear(&mut self) { pub fn clear(&mut self) {
while let Some(_) = self.pop() { } while let Some(_) = self.pop() { }