Repository navigation
Tracking Issue for core::mem::DropGuard #144426
Copy link
Copy link
Closed
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-ready-to-stabilizeStatus: This is ready to stabilize; it may need a stabilization report and a PRStatus: This is ready to stabilize; it may need a stabilization report and a PRT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(drop_guard)]
This is a tracking issue for the core::mem::DropGuard API: a helper type that makes it easy to create impl Drop destructors inline.
Public API
/// Wrap a value and run a closure when dropped.
pub struct DropGuard<T, F: FnOnce(T)> { ... }
impl<T, F> DropGuard<T, F> {
/// Create a new instance of `DropGuard`.
pub fn new(inner: T, f: F) -> Self { ... }
/// Consumes the `DropGuard`, returning the wrapped value.
pub fn dismiss(guard: Self) -> T { ... }
}
impl<T, F> Deref for DropGuard<T, F> {
type Target = T;
fn deref(&self) -> &T { ... }
}
impl<T, F> DerefMut for DropGuard<T, F> { ... }
impl<T, F> Debug for DropGuard<T, F> { ... }
impl<T, F> Drop for DropGuard<T, F> { ... }Example
#![feature(drop_guard)]
use std::mem::DropGuard;
{
// Create a new guard around a string that will
// print its value when dropped.
let s = String::from("Chashu likes tuna");
let mut s = DropGuard::new(s, |s| println!("{s}"));
// Modify the string contained in the guard.
s.push_str("!!!");
// The guard will be dropped here, printing:
// "Chashu likes tuna!!!"
}Steps / History
(Remember to update the S-tracking-* label when checking boxes.)
- ACP: ACP: Add core::mem::WithDrop to associate a custom drop fn with some data libs-team#622
- Implementation: Add
core::mem::DropGuard#144236 - Method name update: Rename
DropGuard::into_innertoDropGuard::dismiss#148589 (comment) - Use in various places in
std: Usedrop_guardin some places in {core,alloc,std} #161702 - Make the
dropmethod#[inline]: Mark<mem::DropGuard as Drop>::dropas#[inline]#162064 - Final comment period (FCP)1
- Stabilization PR Stabilize
core::mem::DropGuard#161520
Unresolved Questions
- What should the name of the type be?
DropGuard,WithDropandDropperwere some of the preferred options raised in the 2025-07-22 T-Libs-API call. - What should the name of the method be for "take ownership of the value without running the destructor"? The name current name for this method is
into_inner, but other names have been proposed, including:disarm,defuse,disable, andskip_drop. - Should the impl have a
Cloneimpl? This can always be added in the future, but should be discussed further prior to stabilization. - Should the
Debugimpl be transparent? This can always be added in the future, but should be discussed further prior to stabilization.
Footnotes
Reacted by orzogc, zopsicle, Laine Taffin Altman, tison, ahoyiski, Cathal, Chaoses-Ib, Connor Gray, Simon Wülker, Robin Lambertz and 8 more
Reacted by lue
Reacted by marziply and Timur Tugushev
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-ready-to-stabilizeStatus: This is ready to stabilize; it may need a stabilization report and a PRStatus: This is ready to stabilize; it may need a stabilization report and a PRT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
[-]Tracking Issue for `std::mem::DropGuard`[/-][+]Tracking Issue for `core::mem::DropGuard`[/+]RalfJung commentedon Jul 28, 2025
Another open question: what should
into_innerbe called?disarmmight be a more evocative name.yoshuawuyts commentedon Jul 28, 2025
I've added it as an open question, and listed some of the other options people have brought up as well.
I should add however that I personally dislike any names that evoke imagery of weapons and violence. If we can at all avoid that, I think we should.
RalfJung commentedon Jul 28, 2025
That is fair. I just think that
into_innerhas the significant downside of being a very common method name, so one could easily think one sees a call to something else -- or one could refactor code to change the type, and theinto_innermay keep working while changing its meaning. I am fine with basically an name that's not a common method name found on many types. :)yoshuawuyts commentedon Jul 28, 2025
Yeah I agree:
into_inneris a fairly inconspicuous name for an operation that is often load-bearing. Having something that more clearly calls attention to itself seems like it might be worth it.FeldrinH commentedon Jul 31, 2025
I don't think this is a problem in practice.
DropGaurd::into_inneris an associated function, not a method, so you have to call it asDropGuard::into_inner(value), which makes it quite hard to confuse it for some otherinto_innerfunction and also makes it a compile error to pass a value of a different type.FeldrinH commentedon Jul 31, 2025
FWIW I like the name
into_inner. It is consistent with the existing scopeguard crate and also consistent with analogous methods on other types that affect drop behavior likeManuallyDropandMaybeUninit.riking commentedon Aug 15, 2025
I think the documentation on
into_innershould also have an example of callingmem::forget(guard.into_inner())sincemem::forgetis mentioned in the prose.karolzwolak commentedon Aug 24, 2025
Implementation PR is merged, so @rustbot label -S-tracking-unimplemented
tisonkun commentedon Nov 25, 2025
Upon
dismissnaming, I wonder if we can avoid requiring pass in a value since amove ||closure would already handle the ownership issue.Then
dismisscan even return().In most cases, the pass-in value is
(), and we'd seldom useDeref/DerefMut.tisonkun commentedon Nov 25, 2025
.. or have an ergonomic simple entry point when the value is
():32 remaining items
danielhenrymantilla commentedon Aug 22, 2026
Regarding naming1,
.dismantle()might be what I've found most befitting. Indeed it is a word which conveys both:.into_inner()deconstruction that it results into.And it is not itself weaponry-related (you can dismantle an operation, for instance).
@ais523 Regarding the drop glue, I think that an API along the lines of https://docs.rs/safe-manually-drop would fit it better, insofar the drop logic would be written as an
implrather than as a closure inside the constructor. Though perhaps we could unify both ideas by using a proxy trait for the second type parameter of theDropGuard, like this:This would retain compatibility with the current API, but would also allow your use-case, @ais523:
Click to see
which makes the drop glue stand out more (à la
impl Drop), than in:Though after writing all this I am starting to think there may be more clarity in having both
SafeManuallyDropandDropGuardthan trying to shoehorn the two together, since I am not 100% fond of thePD<…>layer withDropGuard(and also, the "non-entailedFnOnce" would probably hamper closure arg inference in the more typicalDropGuardintended case of the OP).Footnotes
(as I have not seen any explicit mentions/considerations in the latest minutes of the candidate names suggested on Zulip) ↩
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Rollup merge of rust-lang#161702 - GrigorenkoPV:drop-guard, r=clarfon…
Unrolled build for #161702 in rollup 162028
Rollup merge of rust-lang#161520 - yoshuawuyts:stabilize-dropguard, r…
Unrolled build for #161520 in rollup 162707
Rollup merge of rust-lang#164414 - theemathas:let_underscore_drop_gua…
Rollup merge of rust-lang#164414 - theemathas:let_underscore_drop_gua…