Skip to content

Commit

Permalink
Unrolled build for rust-lang#126118
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#126118 - jan-ferdinand:docs_for_vec_set_len, r=the8472

docs: Mention `spare_capacity_mut()` in `Vec::set_len`

I recently went down a small rabbit hole when trying to identify safe use of `Vec::set_len`. The solution was `Vec::spare_capacity_mut`. I think the docs on `Vec::set_len` benefit from mentioning this method.

A possible counter-argument could be that the [clippy lint `uninit_vec`](https://rust-lang.github.io/rust-clippy/master/index.html#/uninit_vec) already nudges people in the right direction. However, I think a working example on `Vec::set_len` is still beneficial.

Happy to hear your thoughts on the matter. 😊
  • Loading branch information
rust-timer authored Dec 20, 2024
2 parents 5dfe648 + 07ba1fd commit e20cdb4
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,10 @@ impl<T, A: Allocator> Vec<T, A> {
///
/// # Examples
///
/// This method can be useful for situations in which the vector
/// See [`spare_capacity_mut()`] for an example with safe
/// initialization of capacity elements and use of this method.
///
/// `set_len()` can be useful for situations in which the vector
/// is serving as a buffer for other code, particularly over FFI:
///
/// ```no_run
Expand Down Expand Up @@ -1884,6 +1887,8 @@ impl<T, A: Allocator> Vec<T, A> {
///
/// Normally, here, one would use [`clear`] instead to correctly drop
/// the contents and thus not leak memory.
///
/// [`spare_capacity_mut()`]: Vec::spare_capacity_mut
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn set_len(&mut self, new_len: usize) {
Expand Down

0 comments on commit e20cdb4

Please sign in to comment.