@@ -1442,28 +1442,38 @@ impl<T, A: Allocator> Vec<T, A> {
1442
1442
1443
1443
/// Converts the vector into [`Box<[T]>`][owned slice].
1444
1444
///
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.
1446
1449
///
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
1447
1455
/// [owned slice]: Box
1456
+ /// [memory-fitting]: Allocator#memory-fitting
1448
1457
/// [`shrink_to_fit`]: Vec::shrink_to_fit
1458
+ /// [`into_vec`]: Box<[T]>::into_vec
1449
1459
///
1450
1460
/// # Examples
1451
1461
///
1462
+ /// Basic conversion:
1452
1463
/// ```
1453
1464
/// let v = vec![1, 2, 3];
1454
- ///
1455
1465
/// let slice = v.into_boxed_slice();
1456
1466
/// ```
1457
1467
///
1458
- /// Any excess capacity is removed:
1459
- ///
1468
+ /// Preserved allocation size when converting back:
1460
1469
/// ```
1461
1470
/// let mut vec = Vec::with_capacity(10);
1462
1471
/// vec.extend([1, 2, 3]);
1463
1472
///
1464
- /// assert!(vec.capacity() >= 10);
1465
1473
/// 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);
1467
1477
/// ```
1468
1478
#[ cfg( not( no_global_oom_handling) ) ]
1469
1479
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments