Why would one put comma after the last field?


I encountered several times “,” after the last field like in:

struct A {
   field: String,

Is it only for convenience of adding new fields? Deleting seems to do nothing harmful.


Yes, pretty much. And consistency. All fields will look the same when they’re added like this.


More specifically, to make diff view of version control systems more useful. If you have to add comma, then the diff will be noisy because of an extra line.


These commas when added ensures that when look at a diff, you only see the new entry added to the list and not the previous element (since you added the comma)


@mbn [Edit: In addition to the reasons already provided, Rust was also] designed to make it easier for Rust source code to be emitted by automation (ie. another program). It can be tricky sometimes to write the logic to know whether to include a comma if a comma is only legal if there is an additional element (this requires some form of lookahead). To keep things simple, a trailing comma is permitted.

In practice, using a trailing comma makes it easy for humans to add another element to an existing list of, say, struct properties, so it turns out to be handy in practice, too, which is why you see it so commonly.


Hm, interesting! I’ve never heard this justification before, could you provide a link to source?


I don’t have any handy links to the source, but I would say that that
statement is a little strong, but also not wrong. What I would say is,
we allow some syntax constructs that make codegen easier. For example, long
ago, if you had a unit struct, you had to write struct Foo;. But now, you
can also write struct Foo { }. This way, someone doing codegen doesn’t
have to worry about two different forms for unit vs non-unit structs. Etc.


To clarify, codegen here mainly refers to macros, right? I do remember that we allow trait Foo: {} in the parser for this reason :slight_smile: I’ve just never heard this about commas specifically.


That is also the reason why I to it. Also, I sometimes copy field lines to add more, or rearrange fields using shortcuts (I’m actually pretty often moving lines, also outside structs). In Atom, which is currently my preferred editor, the shortcuts are ctrl+direction to move and ctrl+shift+D to duplicate. Then, if I additionally need to add or remove commas, the diff looks more messy than it needs to.


yeah, mostly macros


This is from my recollection of research I have done on Rust over the past couple of years. I will try to find the source.


You are certainly closer to this than I am, so I would take your word on these things over my pourous memory. :slight_smile: Still, I’ll try to find the source where I read this–it might have been on an old email DL where Graydon and co. were talking–I’m not sure. But it might be fun to try to find it again (I remember learning lots of insights into how Rust got to be the way it is along the way).


I think we should move that discussion to another thread


To be clear, I did not intend to imply that ‘code generation’ was the only, or even the main reason that Rust supports trailing commas. But in re-reading my post, I can see where one could easily come away with that impression (I’ve edited my original post to address this). Thank you, @steveklabnik for the heads up!

About a year ago, in an effort to better understand how we’ve arrived where we are today, I was looking into some of the early work that Graydon Hoare did in Rust, and I came across that information.

@matklad, I’ve Google searched, looked through my notes and even dug through my browser history to try to find the link, but have not been successful. :frowning: In my searches, I found that several other languages also provide (varying degrees) of trailing comma support (here is Scala’s rationale, for example, listing all the reasons cited here, including code generation).

If I ever do come across the link for Rust again, though, I will post it here. :slight_smile:

All the best,


Coming from a JavaScript background, I was quite pleased to see Rust allows a comma after the last field.
In JavaScript, that’s a syntax error, and I can’t tell you how many times I had to go back and remove a last comma after trying to run code unsuccessfully.


I wonder why you say that. Javascript has supported trailing commas in arrays and objects for a while. From what I recall, IE8 was the last one not to support it on object literals. JSON does not support trailing commas at all, however.


Well, apparently I was wrong! I’m happy to learn JavaScript supports trailing commas. I thought for sure I’ve had syntax errors arising from that, but maybe it’s been more from not putting a comma after a field, or it could have been in JSON.

Anyway, glad Rust supports trailing commas too.