Skip to content

update doc for Vec::into_boxed_slice#137546

Open
heiseish wants to merge 1 commit intorust-lang:mainfrom
heiseish:doc-125941
Open

update doc for Vec::into_boxed_slice#137546
heiseish wants to merge 1 commit intorust-lang:mainfrom
heiseish:doc-125941

Conversation

@heiseish
Copy link
Contributor

Context

  • Update the doc for Vec::into_boxed_slice to highlight the API dependency on the memory layout strategy, and the doctest to remove the "encoding" of exact capacity guarantee.

Related issue

r? @the8472

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 24, 2025
@workingjubilee
Copy link
Member

also note #135933

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

/// assert_eq!(slice.into_vec().capacity(), 3);
/// let new_vec = slice.into_vec();
/// // The allocator may have kept extra capacity:
/// assert!(new_vec.capacity() >= 3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about the test. I tried many times with assert_eq!(slice.into_vec().capacity(), 3);, it all passed. And I tested it for different types and cases. Only ZST kept extra capacity. Experiment result is here.

@Dylan-DPC
Copy link
Member

r? libs

@rustbot rustbot assigned jhpratt and unassigned the8472 Feb 6, 2026
@jhpratt
Copy link
Member

jhpratt commented Feb 7, 2026

Stepping off rotation for a bit

@rustbot reroll

@rustbot rustbot assigned Mark-Simulacrum and unassigned jhpratt Feb 7, 2026
///
/// When converting back to a vector using `Box<[T]>::into_vec`, the resulting
/// vector may retain this extra capacity. For details about allocator behavior,
/// see [`Allocator::shrink`] and the [memory fitting] documentation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit confusing to me. Rust allocators don't support asking for the capacity, so conversion to Box<[T]> and into_vec can't have capacity > length (at least for non-ZST cases, where we're actually using the allocator).

The underlying allocator may, of course, not need to copy any memory when realloc / grow are called, growing in place, but that's an implementation detail of the allocator.

For example, "final memory layout depends on the allocator" is wrong (I think). The memory layout and provenance of the Box<[T]> returned is valid only for its length and is fixed regardless of the allocator; you can't say that because conversion to Vec later allowed you to resize in place the additional bytes were always usable.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants