Skip to content

Commit ae8fa92

Browse files
committed
update doc for Vec::into_boxed_slice
1 parent 617aad8 commit ae8fa92

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

library/alloc/src/vec/mod.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1442,28 +1442,38 @@ impl<T, A: Allocator> Vec<T, A> {
14421442

14431443
/// Converts the vector into [`Box<[T]>`][owned slice].
14441444
///
1445-
/// Before doing the conversion, this method discards excess capacity like [`shrink_to_fit`].
1445+
/// Before the conversion, this will attempt to shrink the vector's allocation to match its length,
1446+
/// but the final memory layout depends on the allocator's [memory fitting][memory-fitting] strategy.
1447+
/// The returned slice will have exactly [`len`] elements, but take note that the underlying allocation
1448+
/// may still contain unused capacity that is safe to use with sized deallocation methods.
14461449
///
1450+
/// When converting back to a vector using [`into_vec`], the resulting
1451+
/// vector may retain this extra capacity. For details about allocator behavior,
1452+
/// see [`Allocator::shrink`] and the [memory fitting] documentation.
1453+
///
1454+
/// [`len`]: Vec::len
14471455
/// [owned slice]: Box
1456+
/// [memory-fitting]: Allocator#memory-fitting
14481457
/// [`shrink_to_fit`]: Vec::shrink_to_fit
1458+
/// [`into_vec`]: Box<[T]>::into_vec
14491459
///
14501460
/// # Examples
14511461
///
1462+
/// Basic conversion:
14521463
/// ```
14531464
/// let v = vec![1, 2, 3];
1454-
///
14551465
/// let slice = v.into_boxed_slice();
14561466
/// ```
14571467
///
1458-
/// Any excess capacity is removed:
1459-
///
1468+
/// Preserved allocation size when converting back:
14601469
/// ```
14611470
/// let mut vec = Vec::with_capacity(10);
14621471
/// vec.extend([1, 2, 3]);
14631472
///
1464-
/// assert!(vec.capacity() >= 10);
14651473
/// let slice = vec.into_boxed_slice();
1466-
/// assert_eq!(slice.into_vec().capacity(), 3);
1474+
/// let new_vec = slice.into_vec();
1475+
/// // The allocator may have kept extra capacity:
1476+
/// assert!(new_vec.capacity() >= 3);
14671477
/// ```
14681478
#[cfg(not(no_global_oom_handling))]
14691479
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)