@@ -342,12 +342,18 @@ impl<T> Vec<T> {
342342 ///
343343 /// * `ptr` needs to have been previously allocated via `String`/`Vec<T>`
344344 /// (at least, it's highly likely to be incorrect if it wasn't).
345- /// * `length` needs to be the length that less than or equal to `capacity`.
345+ /// * `length` needs to be less than or equal to `capacity`.
346346 /// * `capacity` needs to be the capacity that the pointer was allocated with.
347347 ///
348348 /// Violating these may cause problems like corrupting the allocator's
349349 /// internal datastructures.
350350 ///
351+ /// The ownership of `ptr` is effectively transferred to the
352+ /// `Vec<T>` which may then deallocate, reallocate or change the
353+ /// contents of memory pointed to by the pointer at will. Ensure
354+ /// that nothing else uses the pointer after calling this
355+ /// function.
356+ ///
351357 /// # Examples
352358 ///
353359 /// ```
@@ -479,18 +485,45 @@ impl<T> Vec<T> {
479485 }
480486 }
481487
482- /// Shorten a vector to be `len` elements long, dropping excess elements.
488+ /// Shortens the vector, keeping the first `len` elements and dropping
489+ /// the rest.
483490 ///
484491 /// If `len` is greater than the vector's current length, this has no
485492 /// effect.
486493 ///
494+ /// The [`drain`] method can emulate `truncate`, but causes the excess
495+ /// elements to be returned instead of dropped.
496+ ///
487497 /// # Examples
488498 ///
499+ /// Truncating a five element vector to two elements:
500+ ///
489501 /// ```
490502 /// let mut vec = vec![1, 2, 3, 4, 5];
491503 /// vec.truncate(2);
492504 /// assert_eq!(vec, [1, 2]);
493505 /// ```
506+ ///
507+ /// No truncation occurs when `len` is greater than the vector's current
508+ /// length:
509+ ///
510+ /// ```
511+ /// let mut vec = vec![1, 2, 3];
512+ /// vec.truncate(8);
513+ /// assert_eq!(vec, [1, 2, 3]);
514+ /// ```
515+ ///
516+ /// Truncating when `len == 0` is equivalent to calling the [`clear`]
517+ /// method.
518+ ///
519+ /// ```
520+ /// let mut vec = vec![1, 2, 3];
521+ /// vec.truncate(0);
522+ /// assert_eq!(vec, []);
523+ /// ```
524+ ///
525+ /// [`clear`]: #method.clear
526+ /// [`drain`]: #method.drain
494527 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
495528 pub fn truncate ( & mut self , len : usize ) {
496529 unsafe {
0 commit comments