Wrap to minimum number of lines


#1

The main issue I’ve encountered with rustfmt is the lack of a way to specify that function calls should be wrapped to the minimum number of lines, which is the default style (for C/C++ at least) in the code bases I’m used to. In particular:

  • If a function call requires multiple lines, rustfmt puts each argument on a separate line. Instead, I’d like to have multiple parameters on the same line, up to the max characters per line.

  • Sometimes, the opening parenthesis of a function call is quite close to the right edge column. Often in such cases to wrap the statement to the minimum number of lines rustfmt should wrap the first argument to the next line.

Example:

// unwanted
let very_long_name = very_long_function_name(param1,
                                             param2,
                                             param3);

// better alternative
let very_long_name = very_long_function_name(
                         param1, param2, param3);

// even better alternative
let very_long_name =
    very_long_function_name(param1, param2, param3);

The same concerns apply to macro invocations and other similar constructs. If there is a way to get this “wrap to minimum number of lines” then I think I could enforce auto-formatting for my code.

And, it probably goes without saying that I think such mode should be the default.


#2

I personally prefer the winapi style because it is consistent in how much indentation is used which means it is incredibly easy to type manually and refactoring doesn’t cause excessive diff noise.

let very_long_name = very_long_function_name(
    param1, param2, param3,
);

#3

There is also an incomplete but concrete alternative style guide based on the winapi style. (I believe that the author, @ubsan, intends this to be public, as the gist were public, but please let me know if it’s not the case.)


#4

This is the updated link, which is far more complete :slight_smile: