Skip to content

Commit

Permalink
Partial rewrite/expansion of Vec::truncate documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Jul 16, 2016
1 parent dc8212f commit e2f5961
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/libcollections/vec.rs
Expand Up @@ -479,18 +479,45 @@ impl<T> Vec<T> {
}
}

/// Shorten a vector to be `len` elements long, dropping excess elements.
/// 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.
///
/// The [`drain`] method can emulate `truncate`, but causes the excess
/// elements to be returned instead of dropped.
///
/// # Examples
///
/// Truncating a five element vector to two elements:
///
/// ```
/// let mut vec = vec![1, 2, 3, 4, 5];
/// vec.truncate(2);
/// assert_eq!(vec, [1, 2]);
/// ```
///
/// No truncation occurs when `len` is greater than the vector's current
/// length:
///
/// ```
/// let mut vec = vec![1, 2, 3];
/// vec.truncate(8);
/// assert_eq!(vec, [1, 2, 3]);
/// ```
///
/// Truncating when `len == 0` is equivalent to calling the [`clear`]
/// method.
///
/// ```
/// let mut vec = vec![1, 2, 3];
/// vec.truncate(0);
/// assert_eq!(vec, []);
/// ```
///
/// [`clear`]: #method.clear
/// [`drain`]: #method.drain
#[stable(feature = "rust1", since = "1.0.0")]
pub fn truncate(&mut self, len: usize) {
unsafe {
Expand Down

0 comments on commit e2f5961

Please sign in to comment.