-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite/expand doc examples for
Vec::set_len
.
- Loading branch information
Showing
1 changed file
with
30 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
frewsxcv
Author
Member
|
||
/// } | ||
/// ``` | ||
/// | ||
/// 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] | ||
|
There is no leak here.
char
is a copy type, so there is no missing drop call. You can get a leak withvec![vec![2], ...]