File tree 1 file changed +30
-2
lines changed
1 file changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -532,9 +532,37 @@ impl<T> Vec<T> {
532
532
/// # Examples
533
533
///
534
534
/// ```
535
- /// let mut v = vec![1, 2, 3, 4];
535
+ /// use std::ptr;
536
+ ///
537
+ /// let mut vec = vec!['r', 'u', 's', 't'];
538
+ ///
539
+ /// unsafe {
540
+ /// ptr::drop_in_place(&mut vec[3]);
541
+ /// vec.set_len(3);
542
+ /// }
543
+ /// assert_eq!(vec, ['r', 'u', 's']);
544
+ /// ```
545
+ ///
546
+ /// In this example, there is a memory leak since the memory locations
547
+ /// owned by the vector were not freed prior to the `set_len` call:
548
+ ///
549
+ /// ```
550
+ /// let mut vec = vec!['r', 'u', 's', 't'];
551
+ ///
552
+ /// unsafe {
553
+ /// vec.set_len(0);
554
+ /// }
555
+ /// ```
556
+ ///
557
+ /// In this example, the vector gets expanded from zero to four items
558
+ /// without any memory allocations occurring, resulting in vector
559
+ /// values of unallocated memory:
560
+ ///
561
+ /// ```
562
+ /// let mut vec: Vec<char> = Vec::new();
563
+ ///
536
564
/// unsafe {
537
- /// v .set_len(1 );
565
+ /// vec .set_len(4 );
538
566
/// }
539
567
/// ```
540
568
#[ inline]
You can’t perform that action at this time.
0 commit comments