Rustfmt: skip one option

How do I tell rustfmt to skip just one of its options, for just one struct or enum?

Specifically, I don't want:

pub enum FilterCoefficient {
    FilterOff = 0b000,
    Coeff2    = 0b001,
    Coeff4    = 0b010,
    Coeff8    = 0b011,
    Coeff16   = 0b100,
}

To turn into:

pub enum FilterCoefficient {
    FilterOff = 0b000,
    Coeff2 = 0b001,
    Coeff4 = 0b010,
    Coeff8 = 0b011,
    Coeff16 = 0b100,
}

I know about #[rustfmt::skip] but that skips everything, I would like something like #[rustfmt::cfg(struct_field_align_threshold = 20)] but I can't get anything like that to work.

Thanks,

-kb

P.S. Yes, I know, I'm not supposed to want it to be clear what going on in that second column by having it aligned in a way that the human eye can quickly scan, I know that in that code is supposed to be squished left and made hard to read.

#[rustfmt::skip] is all you get within a source file. (Or more precisely, there are some finer controls but they’re about what parts not to format, not what rules not to apply.) However, if you’re willing to use unstable rustfmt options, you can have rustfmt maintain the formatting you want; in a rustfmt.toml config file you’d write

enum_discrim_align_threshold = 40

to set the maximum variant name width it’ll align to (the default is zero, so no alignment). But you’ll need to use nightly rustfmt to do this.

https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#enum_discrim_align_threshold

1 Like

So in my case, #[rustfmt::skip] on the code I want to format myself, is what I want.

Thanks,

-kb