const-destructure

Crates.ioconst-destructure
lib.rsconst-destructure
version0.1.3
created_at2025-10-08 02:43:00.585173+00
updated_at2025-10-09 21:25:53.343867+00
descriptionDestructuring in const contexts on stable Rust.
homepage
repositoryhttps://github.com/mickvangelderen/const-destructure
max_upload_size
id1873324
size24,247
Mick van Gelderen (mickvangelderen)

documentation

README

This crate provides a macro that allows you to destructure types in a const context:

struct Wrap<T> {
    value: T
}

impl<T> Wrap<T> {
    const fn into_inner(self) -> T {
        const_destructure!(let Self { value } = self);
        value
    }
}

Unlike normal destructuring, the macro enforces that you assign all fields. If it didn't, it would be possible to accidentally leak unassigned fields because of how the macro is implemented. This is still a big win over writing the implementation by hand, which requires unsafe.

Motivation

The following doesn't compile on rustc 1.90.0 (2025) due to https://github.com/rust-lang/rust/issues/86897:

struct Wrap<T> {
    value: T
}

impl Wrap<T> {
    const fn into_inner(self) -> T {
        let Self { value } = self;
        value
    }
}
error[E0493]: destructor of `Wrap<T>` cannot be evaluated at compile-time
 --> test.rs:6:25
  |
6 |     const fn into_inner(self) -> T {
  |                         ^^^^ the destructor for this type cannot be evaluated in constant functions
...
9 |     }
  |     - value is dropped here

You get the same error without destructuring:

impl<T> Wrap<T> {
    pub const fn into_inner(self) -> T {
        self.value
    }
}
error[E0493]: destructor of `Wrap<T>` cannot be evaluated at compile-time
 --> test.rs:6:25
  |
6 |     const fn into_inner(self) -> T {
  |                         ^^^^ the destructor for this type cannot be evaluated in constant functions
7 |         self.value
8 |     }
  |     - value is dropped here
Commit count: 0

cargo fmt