Rustfmt: single-line comment after a trailing comment

rustfmt seems to be taking care of alignment of trailing comments.
I think the behavior breaks the correct indentation of a single-line comment after a trailing comment.

Here is the code from my attempt on a Rustling.

fn array_and_vec() -> ([i32; 4], Vec<i32>) {
    let a = [10, 20, 30, 40]; // a plain array
    // let v = vec!(10, 20, 30, 40);
    let v = a.to_vec();

    (a, v)
}

Then, I'm trying to run rustfmt to all my attempts, to familiarize myself with the recommended style.
But for this specific code, rustfmt is suggesting something that I cannot accept.

 fn array_and_vec() -> ([i32; 4], Vec<i32>) {
     let a = [10, 20, 30, 40]; // a plain array
-    // let v = vec!(10, 20, 30, 40);
+                              // let v = vec!(10, 20, 30, 40);
     let v = a.to_vec();

     (a, v)

Am I violating something with the original form of the comment?
For example, would it be generally recommended to force a blank line before the line comment?

Version: rustfmt 1.7.0-stable (82e1608 2023-12-21).

In my experience, comments at the end of a line a very rare.

Agree. Maybe you can configure your editor to use block comment (/*...*/) to comment out code instead? Otherwise, I suggest putting a blank line in between.

I myself tend to spare inline comments. They are likely to be either obvious or not meant to be crushed into the margin.

I guess the most sensible use is in pedagogy, which is happening here.

I tried using a block comment after your suggestion. While I expected that a block comment could be another clever workaround, it turns out to surprisingly not work.

 fn array_and_vec() -> ([i32; 4], Vec<i32>) {
     let a = [10, 20, 30, 40]; // a plain array
-    /* let v = vec!(10, 20, 30, 40); */
+                              /* let v = vec!(10, 20, 30, 40); */
     let v = a.to_vec();

     (a, v)

I'm being inclined to suspect that this behavior is an implementation bug.

If I were to write a comment at the end of line and it doesn't fit my preferred amount of columns so I'd have to split it across two, I'd write it exactly like rustfmt formats it. It's just a matter of taste which I don't consider a bug. Ever code formatter is opinionated. I guess if you only refer to the block comment being indented that there are enough people finding that weird enough so that the behaviour could be adjusted.

This may be self-fulfilling prophecy due to lack of rustfmt support.

I tend to use them to describe why if condition is the way it is, district from comment on the whole if/else block or code inside it.

5 Likes

I see the use case you mentioned is certainly valid and syntactically ambiguous to the form I intend. That case should be exactly the reason why this behavior is here. Then, I would hope that rustfmt be agnostic rather than forcing.

The case with block comment is certainly weird. I guess that one could slip since the block comments themselves are not popular?

Fair. I quickly looked through the available config options but haven't found something to disable the indentation of comments only. I assume what you are experiencing falls under the indent_style option, but that would be far too broad to change something as niche as don't-indent-comments-after-an-end-of-line-comment.

Could be. There's even an option to normalize block comments to single-line comments.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.