2

I'm exploring ways that I can nicely format const tables of data to be more compact and easier to read. For example, I have [[u64; 64]; 2] where rustfmt just spaghettifies into hundreds of lines. It would be nice to format into something more compact, like this:

enter image description here

I have other tables of similar types, like [[u64; 64]; 64], ([(u64, u64, u32, usize); 64], [u64; 1024]).

Does rustfmt have attributes that can help, for example? I know #![rustfmt::skip] but don't see mention of other ones. And this one doesn't really help since this means I'll be manually formatting lots of tables.

Other than writing custom code for each one to generate in a more compact format, is there a tool that can perhaps help automate this?

  • Would disabling Rust fmt for these lines of code work? https://stackoverflow.com/questions/67288537/how-can-i-switch-off-rustfmt-for-a-region-of-code-instead-of-a-single-item – Nick Dec 27 '22 at 16:58
  • @Nick Thanks for suggestion. I'm using that now, but it means a lot of manual work because I have many tables and generating more as I go. –  Dec 27 '22 at 16:59

1 Answers1

1

There is no way to direct rustfmt to format specific code blocks differently. In my project, I use #[rustfmt::skip] on data arrays like these.

However, in this case, you may be able to get what you want using the rustfmt option short_array_element_width_threshold. Setting that option to 18, up from the default of 10, in a rustfmt.toml

short_array_element_width_threshold = 18

results in rustfmt producing the following formatting (with the default 100 character line width):

pub const X: [[u64; 64]; 2] = [
    [
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
    ],
    [
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
        0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF, 0x0123456789ABCDEF,
    ],
];
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • This is pretty close! Thanks. Too bad I can't apply rustfmt only on certain files. –  Dec 27 '22 at 18:43