`const` destructuring

I want to declare multiple const variables in the same line. For let I use destructuring:

let (a, b) = (1, 2);

Is this possible with const?


const (A, B): (i32, i32) = (1, 2);
   Compiling playground v0.0.1 (/playground)
error: expected identifier, found `(`
 --> src/lib.rs:1:7
1 | const (A, B): (i32, i32) = (1, 2);
  |       ^ expected identifier

You could sidestep this with a macro to make it all fit on 1 line. The macro would properly declare each const on its own line.

It doesn't appear to be possible, the syntax binds identifiers, and not patterns. https://doc.rust-lang.org/reference/items/constant-items.html, you'll need to bind it to a temporary tuple first and destructure it manually, perhaps using a module to hide the temporary tuple binding?

mod _tmp {
  const X : (u32, u32) = (0, 0);
  pub const Y : u32 = X.0;
  pub const Z : u32 = X.1;

use _tmp::*;
1 Like