Trailing Commas in Matches, Structs, &c

Here we have an open style question, on whether trailing commas in such a statement as this:

Foo {
    bar: T,
    baz: T,

should be the default. I, personally, find this ugly in comparison to

Foo {
    bar: T,
    baz: T

What are everyone’s thoughts on this?

I prefer ending each item with a comma.

From an aesthetic point of view it arguably looks more balanced. The comma also marks the end of the item similarly to what semicolons do for statements.

On a practical note. If you add an item to the list you won’t have to go back and add a comma to the line before. This makes commit diffs less noisy.


As @arcto says, trailing commas on the final item makes line re-ordering/adding easier, and looks more balanced.

1 Like

I agree with having commas at the end of every line. That has been supported in most constructs for a while now, and I find it much more convenient than having a first compiler-run just to fix the commas after some larger changes.

How could they forget about array patterns! : )

fn f<T, U, V,>(a: u8, b: u16, c: u32,) -> ([int, ..3], int, int,) { ([1, 2, 3,], 4, 5,) }

fn main() {
    let ([a, b, c/*,*/], d, e,) = f::<i8, i16, i32,>(1, 2, 3,);

trailing comma all the things, imho, for the same reasons everyone else has said.

1 Like

I always add trailing commas too. It makes items more independent from one another - the last item doesn't have to "know" it's the last item.

If the items are on separate lines, put a comma after each of them. This makes it easier to move things around, and doesn’t yield a ,] construct.

If the items are on a single line, don’t put the last comma, it’s in line with math notation (you don’t see S = {1, 2, 3,} in math, and moving things around always modifies the line anyway, no need to keep the extra comma.

EDIT (from below): The only exception to this is the one-element tuple, where the comma is required.

Thanks @phaylon.


Except for single-item tuples, where you need the trailing comma, of course.

/agree with trailing commas.

They should continue to be completely optional for one line or multi-line values. This is a huge win in every way.

I agree with all the objective benefits of the trailing comma, but aesthetically, I think it looks wrong.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.