Skip to content

Commit 9f3ccd4

Browse files
author
Yuki Okushi
authored
Rollup merge of #105032 - HintringerFabian:improve_docs, r=JohnTitor
improve doc of into_boxed_slice and impl From<Vec<T>> for Box<[T]> Improves description of `into_boxed_slice`, and adds example to `impl From<Vec<T>> for Box<[T]>`. Fixes #98908
2 parents 52e8862 + f9490c8 commit 9f3ccd4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

library/alloc/src/vec/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,8 @@ impl<T, A: Allocator> Vec<T, A> {
10701070

10711071
/// Converts the vector into [`Box<[T]>`][owned slice].
10721072
///
1073-
/// Note that this will drop any excess capacity.
1073+
/// If the vector has excess capacity, its items will be moved into a
1074+
/// newly-allocated buffer with exactly the right capacity.
10741075
///
10751076
/// [owned slice]: Box
10761077
///
@@ -3223,6 +3224,14 @@ impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> {
32233224
/// ```
32243225
/// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice());
32253226
/// ```
3227+
///
3228+
/// Any excess capacity is removed:
3229+
/// ```
3230+
/// let mut vec = Vec::with_capacity(10);
3231+
/// vec.extend([1, 2, 3]);
3232+
///
3233+
/// assert_eq!(Box::from(vec), vec![1, 2, 3].into_boxed_slice());
3234+
/// ```
32263235
fn from(v: Vec<T, A>) -> Self {
32273236
v.into_boxed_slice()
32283237
}

0 commit comments

Comments
 (0)