@@ -342,12 +342,18 @@ impl<T> Vec<T> {
342
342
///
343
343
/// * `ptr` needs to have been previously allocated via `String`/`Vec<T>`
344
344
/// (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`.
346
346
/// * `capacity` needs to be the capacity that the pointer was allocated with.
347
347
///
348
348
/// Violating these may cause problems like corrupting the allocator's
349
349
/// internal datastructures.
350
350
///
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
+ ///
351
357
/// # Examples
352
358
///
353
359
/// ```
@@ -479,18 +485,45 @@ impl<T> Vec<T> {
479
485
}
480
486
}
481
487
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.
483
490
///
484
491
/// If `len` is greater than the vector's current length, this has no
485
492
/// effect.
486
493
///
494
+ /// The [`drain`] method can emulate `truncate`, but causes the excess
495
+ /// elements to be returned instead of dropped.
496
+ ///
487
497
/// # Examples
488
498
///
499
+ /// Truncating a five element vector to two elements:
500
+ ///
489
501
/// ```
490
502
/// let mut vec = vec![1, 2, 3, 4, 5];
491
503
/// vec.truncate(2);
492
504
/// assert_eq!(vec, [1, 2]);
493
505
/// ```
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
494
527
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
495
528
pub fn truncate ( & mut self , len : usize ) {
496
529
unsafe {
0 commit comments