rustformattingcommentsrustfmt

rustfmt: disable formatting for comment


I have the following piece of code:

/*
long explanatory comment with some ascii art etc.
*/
#[test]
fn foo() {
    //...
}

I want to only disable rustfmt for my leading comment, so I tried

#[rustfmt::skip]
/*
long explanatory comment with some ascii art etc.
*/
#[test]
fn foo() {
    // I want this to still be formatted.
}

but unfortunately that disabled formatting for the entire function.

Is there a feature to only disable formatting for the comment? I'm thinking of something like the //clang-format: off comments.


Solution

  • AFAIK, there is no such thing at the moment. But you can always workaround it in this way:

    #[rustfmt::skip]
    /*
    long explanatory comment with some ascii art etc.
    */
    #[test]
    fn foo() {
        foo_impl();
    }
    
    #[cfg(test)]
    fn foo_impl() {
        // I want this to still be formatted.
    }