Destructing in const statement?

I noticed that destructing is not support in const statement. Doing something like

const (A, B): (usize, usize) = (1, 2);

would cause

error: expected identifier, found `(`

This can occasionally be useful for handling output of macro, for example,

macro_rules! include_name_and_content {
    ($f:literal) => { ($f, include_str!($f)) }
}
const (FILENAME, CONTENT): (&str, &str) = include_name_and_content!("a");

and soon it might be useful with const fn as well.

I wonder whether there is any reason not to support destructing in const? Or maybe it's just never proposed and thus not implemented?

1 Like

I suspect it's on the list of things to do. From the reading I've done, there's a lot of gotchas in implementing const stuff in Rust, and so while pushing for a large subset of rust to be supported, what you can do in const statements has been expanding very slowly.

Note that there's no such thing as a "const statement". const Ident: Type = Expr; is an item (as in the macro fragment) form and not a stmt. Moreover, bindings must be declared before use whereas constants can be declared after use:

fn foo() {
    dbg!(X);
    const X: u8 = 0; // OK.

    dbg!(x); // ERROR.
    let x: u8 = 0;
}

I wonder would this need to go through RFC as well if we ever add it? I suppose it does?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.