Repository navigation
Open
Labels
A-boxArea: Our favorite opsem complicationC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.S-tracking-design-concernsStatus: There are blocking design concerns.S-waiting-on-concernsStatus: Awaiting concerns to be addressed by the authorT-libsRelevant to the library team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
Description
Feature gate: #![feature(box_into_inner)]
This is a tracking issue for consuming a Box and returning its wrapped value.
The language actually supports *x as a special case. However it can't be generalized to other types easily. It also doesn't consume the Box value immediately if T is Copy, which is quite a footgun.
An explicit version is added here to make the API more consistent with Pin, Cell, RefCell etc.
Public API
impl<T, A: Allocator> Box<T, A> {
pub fn into_inner(boxed: Self) -> T;
}Steps / History
- Implementation: Add
Box::into_inner. #80438 - Improve documentation for
Box::into_inner#98523 - Final commenting period (FCP)
- Stabilization PR
Unresolved Questions
- None yet.
Reacted by Nazar Mokrynskyi, Luke, Michael Pfaff, Joshua L., endeavor, Ben, Denis Kotlyarov, Tianyi Shi, Kart, Antonio Yang and 73 more
Metadata
Metadata
Assignees
Labels
A-boxArea: Our favorite opsem complicationC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.S-tracking-design-concernsStatus: There are blocking design concerns.S-waiting-on-concernsStatus: Awaiting concerns to be addressed by the authorT-libsRelevant to the library team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
Activity
fosskers commentedon Aug 20, 2021
How come it's a function and not a method?
crlf0710 commentedon Sep 7, 2021
@fosskers https://rust-lang.github.io/api-guidelines/predictability.html#smart-pointers-do-not-add-inherent-methods-c-smart-ptr
conradludgate commentedon Oct 30, 2021
When looking at how to turn a box back into a stack owned value, I find this function, but in my case the
*xmethod works perfectly fine. It's documented in the box module, but I feel like it should be doubly documented here as a simpler alternative.TehPers commentedon Apr 9, 2022
Somewhat minor, but I like that
Box::into_innercan be used in method chains without requiring a closure..map(|x| *x)is still shorter than.map(Box::into_inner), but the latter more clearly describes what you're doing in my opinion. It can also help the compiler infer the type in the iterator/Option<T>/whatever at that point in the method chain for what it's worth. Otherwise you'd have to write|x: Box<_>| *xfor the same effect.BurntSushi commentedon May 13, 2022
Box::into_innerappears to have been around for a while, and I don't see any obvious issues with it? What do others think?@rfcbot merge
rfcbot commentedon May 13, 2022
m-ou-se commentedon May 13, 2022
rfcbot commentedon May 13, 2022
77 remaining items
ais523 commentedon Jun 13, 2024
I am trying to think of a concrete set of fixes, but the problem is actually the same one that blocked stablisation of this issue earlier:
Box::into_inner(and the competing alternativeBox as DerefMove) aren't actually useful very often, which is why people can spend so long before coming across the special case (I've been programing Rust for years without knowing how to move out of aBox), and why it's hard to create a simple example for use in documentation. Normally, once a value has been placed in aBox, it makes sense to keep it there and not move the value further. (Likewise, althoughselfreceivers are fairly common, they're most commonly used either on types that areCopyand can be copied out of a box normally, or on container types that already store their data on the heap and thus don't need a box.)In case it helps someone: the situation in which I came across this was that I was trying to convert a
Vecof a non-Copytype into an array in stable Rust, and came across the sequence "convert theVecinto a boxed slice, then use theTryFromimplementation between a boxed slice and box array, then unbox the array". Unfortunately, that's probably too complex for use in a simple example.scottmcm commentedon Jun 13, 2024
Also there's been
[T; N]: TryFrom<Vec<T>>since 1.48 (#76310), to do the whole thing in one step.dtolnay commentedon Aug 6, 2024
We discussed this tracking issue in today's standard library API team meeting. The thing it's stuck on is still a PR for #80437 (comment). Some of the comments following that one have some additional suggestions.
BenBwall commentedon Sep 30, 2024
One use case I haven't seen mentioned is when you have a type alias that may or may not point to
Box<T>and you want to get the inner T. This could also be solved by stabilizing MoveDeref, but MoveDeref has been stalled for several years as far as I'm aware, so I think stabilizing this is more possible/actionable in the short to medium term.ActuallyHappening commentedon Feb 8, 2025
Haven't read the whole conversation, but this method was exactly what I was looking for. My mental model tells me that going from an owned pointer to the inner should happen, so looking for this method I was surprised that it wasn't stabilized yet. But
*statewas good enough for the moment :)ActuallyHappening commentedon Apr 5, 2025
Would still like this to be stabalized
theemathas commentedon Sep 15, 2026
A notable thing about this is:
let x = *my_box;does not immediately deallocate the heap memory. The heap memory only gets deallocated whenmy_box(which now contains the "husk" of a box) goes out of scope.In contrast,
let x = Box::into_inner(my_box);immediately deallocates the box, since the entire box is consumed. Emulating this without a function call requires writing the obscure incantation oflet x = *{my_box};Is this a strong reason to stabilize this function?
tgross35 commentedon Sep 20, 2026
With allocator stabilization around the corner, perhaps we should consider making this
-> (T, A)?Skgland commentedon Nov 10, 2026
Box::takeis being stabilized in 1.100 and has-> (T, Box<MaybeUninit<T>, A>)Edit: I somehow misread the signature and thought it returned a triple not a pair.
A248 commentedon Nov 11, 2026
That's not the same, insofar as the allocator isn't returned; you're just turning one Box into another while reading out the memory.
Now that said, I'm not saying Box::into_inner should return the allocator. I think it better satisfies the main motivations on this thread to just return the
T. Following the pattern of other (unstable) methods on Vec, maybe something likeBox::into_inner_with_allocatorcould be a future direction.