Skip to content

Commit

Permalink
Rewrite/expand doc examples for Vec::set_len.
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Jul 19, 2016
1 parent 6aba7be commit a005b2c
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,37 @@ impl<T> Vec<T> {
/// # Examples
///
/// ```
/// let mut v = vec![1, 2, 3, 4];
/// use std::ptr;
///
/// let mut vec = vec!['r', 'u', 's', 't'];
///
/// unsafe {
/// ptr::drop_in_place(&mut vec[3]);
/// vec.set_len(3);
/// }
/// assert_eq!(vec, ['r', 'u', 's']);
/// ```
///
/// In this example, there is a memory leak since the memory locations
/// owned by the vector were not freed prior to the `set_len` call:
///
/// ```
/// let mut vec = vec!['r', 'u', 's', 't'];
///
/// unsafe {
/// vec.set_len(0);

This comment has been minimized.

Copy link
@malbarbo

malbarbo Jul 21, 2016

Contributor

There is no leak here. char is a copy type, so there is no missing drop call. You can get a leak with vec![vec![2], ...]

This comment has been minimized.

Copy link
@frewsxcv

frewsxcv Jul 21, 2016

Author Member

You're absolutely right. Do you want to open a pull request to fix the example? If not, I can do it.

This comment has been minimized.

Copy link
@malbarbo

malbarbo Jul 22, 2016

Contributor

Please, go ahead and fix it.

This comment has been minimized.

Copy link
@frewsxcv

frewsxcv Jul 23, 2016

Author Member

Opened a PR: #34989

/// }
/// ```
///
/// In this example, the vector gets expanded from zero to four items
/// without any memory allocations occurring, resulting in vector
/// values of unallocated memory:
///
/// ```
/// let mut vec: Vec<char> = Vec::new();
///
/// unsafe {
/// v.set_len(1);
/// vec.set_len(4);
/// }
/// ```
#[inline]
Expand Down

0 comments on commit a005b2c

Please sign in to comment.