rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

[unstable option] space_before_colon

Open scampi opened this issue 6 years ago • 3 comments

Tracking issue for unstable option: space_before_colon

scampi avatar Feb 13 '19 22:02 scampi

(Please excuse if this does not belong here)

Is there a way (planned) to distinguish between a parameter list, let statement, and other uses of the type annotation colon?

I would like to have my code formatted so that

let my_string : String = "hello".to_string();

has a space before the colon, because the colon feels like it separates the type from the identifier.
But I would like to have no space before the colon here:

fn foo( my_arg: String, another_arg: &str) {

}

because the type belongs to its preceeding argument and it feels like it is too close to the next parameter when I have the space before the colon.

Of course, tastes may vary. And it's not that important to be able to distinguish these cases. But I'd like to, if it was easily doable.

lucidBrot avatar Dec 20 '19 17:12 lucidBrot

My thoughts as well, but to say it in one rule: The colon belongs with the type when the type is optional. Because they are added or removed together. Just like in UML.

I can think of 3 uses of colon:

  • Before an optional type (those let statements)
  • Before an obligatory type (such as function and struct parameters)
  • Before a value (in designated initializers)

I would also remove the space after the colon (#3366) by the exact same rule, but maybe that's just me.

anordal avatar Feb 22 '20 22:02 anordal

I just think it looks more readable with the space before the colon across the board. ML-heritage showing 😄

effinsky avatar Jan 29 '24 13:01 effinsky