Formatting of medium & long function signatures with rustfmt

There is one thing that bothers me with rustfmt, and it is how it formats long function signatures, making them -- I think -- hard to read.

There are two types of function signature that I'd like to see better formatted, but let's start with the long one:

    fn function(long_name_0: String, long_name_1: String, long_name_2: String, long_name_3: String, long_name_4: String, long_name_5: String) -> HashMap<String, String> { todo!() }

The default rustfmt setting (Tall) will format the function as:

    fn function(
        long_name_0: String,
        long_name_1: String,
        long_name_2: String,
        long_name_3: String,
        long_name_4: String,
        long_name_5: String,
    ) -> HashMap<String, String> {
        todo!()
    }

And my issue with it is that it makes the result type hardly distinguishable form the arguments, when after having spent so many lines for the arguments already, it seems futile to squeeze 2 more out of it.

I'd like for the code to be formatted as:

    fn function(
        long_name_0: String,
        long_name_1: String,
        long_name_2: String,
        long_name_3: String,
        long_name_4: String,
        long_name_5: String,
    )
        -> HashMap<String, String>
    {
        todo!()
    }

Which more cleanly separates the result type from the arguments.

Another, somewhat similar issue, pertains to "medium-size" function signatures, where the arguments could fit on one line, but the result type doesn't, the formatting immediately switches to the first formatting, instead of going with:

    fn function(long_name_0: String, long_name_1: String, long_name_2: String)
        -> HashMap<String, String>
    {
        todo!()
    }

Which costs less line while still respecting width limits and presenting the various pieces in well-delineated parts.

Are there options of rustfmt to:

  • Put the result on its own line, rather than the line of the closing parenthesis, when using the vertical layout?
  • Put the brace to open the function body on its own line as soon as the signature takes multiple lines already?
  • Use the "medium" layout for not-so-long signatures?

A quick looup on the documentation for rustfmt only shows an option to format a functions argument(s):

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

It's unsure if the same option has any effect in the return type.

So, overall I would say that there are no options to customize it as you wish.

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.