# Grammar liberalizations

**URL:** <https://internals.rust-lang.org/t/grammar-liberalizations/7170>\
**Category:** Uncategorized\
**Created:** [March 28, 2018, 6:56pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170 "2018-03-28T18:56:39Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [March 28, 2018, 6:56pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/1 "2018-03-28T18:56:39Z")

</div>

In an effort to make Rust’s grammar more tolerant of macros and people, I’d like to collect a list of grammatical forms that are currently not allowed.

Note that the forms that may be illegal now but possibly legal in the future are not an indication of a recommended one-true-style by rustfmt. Instead, it is a philosophy in Rust language design to have a flexible grammar that accepts things that people find abhorrent.

Some forms that are illegal now are:

1. Leading comma

```rust
fn main() {
    let arr = [
    , 1
    , 2
    , 3
    ];
}

struct Foo {
  , foo: usize
  , bar: u8
}

fn foo<, T>() {}

```

1. Leading plus

```rust
fn foo<T: + Copy>() {}

```

If you can think of any other “strange” forms that are illegal today please make a note of them here.

The end result of this thread will likely be an RFC proposing that some of these forms be legal.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [March 28, 2018, 8:52pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/2 "2018-03-28T20:52:05Z")

</div>

I don’t understand why allowing leading commas would be useful. If you want as many commas as elements/struct fields, then just include a trailing comma after the last one. That is allowed _today_, and it’s also trivial to achieve in machine-generated code.

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [March 28, 2018, 9:04pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/3 "2018-03-28T21:04:28Z")

</div>

> [@H2CO3](#):
>
> I don’t understand why allowing leading commas would be useful.

Simple... Some people prefer that style.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [March 28, 2018, 9:10pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/4 "2018-03-28T21:10:35Z")

</div>

in that case, the decision should also be equally simple: that’s probably not enough justification for adding them to the language.

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [March 28, 2018, 9:21pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/5 "2018-03-28T21:21:29Z")

</div>

I don’t want to litigate this here. Only collect instances where _some people think_ that the grammar is too unforgiving.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [March 29, 2018, 2:45am UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/6 "2018-03-29T02:45:15Z")

</div>

> [@Centril](#):
>
> Instead, it is a philosophy in Rust language design to have a flexible grammar that accepts things that people find abhorrent.

Is this true? The liberalizations I've seen (trailing comma in calls, leading vert in arms, trailing + in bounds, ...) all seem to be aiming for a different philosophy: All items in a sequence can be written the same way, if you so wish, with no need to do something different a particular position. Thus `foo(a, b,)`, `T: A+ B+`, `|A |B =>`, ...

That serves both the code generators wanting to just do the same transformation for each item as well as the [rustfmt principle](https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/principles.md) to prefer formats that result in minimal diffs in source control.

That's very different from supporting different formats because people have different tastes.

---

<div class="post-metadata">

**Author:** ![leonardo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/leonardo/32/1344_2.png) [@leonardo](https://internals.rust-lang.org/u/leonardo)\
**Post date:** [March 29, 2018, 8:15am UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/7 "2018-03-29T08:15:32Z")

</div>

I love the Python part of its Zen:

_There should be one-- and preferably only one --obvious way to do it._

I add a lovely quotation from John Carmack ([https://www.gamasutra.com/view/news/128836/InDepth\_Static\_Code\_Analysis.php](https://www.gamasutra.com/view/news/128836/InDepth_Static_Code_Analysis.php) ):

> This seems to imply that if you have a large enough codebase, any class of error that is syntactically legal probably exists there. In a large project, code quality is every bit as statistical as physical material properties – flaws exist all over the place, you can only hope to minimize the impact they have on your users.

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [March 29, 2018, 12:31pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/8 "2018-03-29T12:31:58Z")

</div>

> [@scottmcm](#):
>
> Is this true?

[Yes, I think it is.](https://github.com/rust-lang/rfcs/pull/1925#issuecomment-325500881) However, let's not go to meta and discuss the premise of the thread please.

---

<div class="post-metadata">

**Author:** ![theotherphil](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/theotherphil/32/3337_2.png) [@theotherphil](https://internals.rust-lang.org/u/theotherphil)\
**Post date:** [March 29, 2018, 12:36pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/9 "2018-03-29T12:36:53Z")

</div>

Describing the current rules as “too unforgiving” _is_ litigating the issue here.

---

<div class="post-metadata">

**Author:** ![kyrias](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kyrias/32/3872_2.png) [@kyrias](https://internals.rust-lang.org/u/kyrias)\
**Post date:** [March 29, 2018, 12:48pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/10 "2018-03-29T12:48:55Z")

</div>

I’m quite interested now though, do you (or anyone else) know of any code in other languages that’s actually written like that?

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [March 29, 2018, 1:12pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/11 "2018-03-29T13:12:31Z")

</div>

> [@kyrias](#):
>
> I’m quite interested now though, do you (or anyone else) know of any code in other languages that’s actually written like that?

[In Haskell it is common to write like so](https://github.com/tibbe/haskell-style-guide/blob/master/haskell-style.md):

```haskell
myList =
  [ 1
  , 2
  , 3
  , 4
  ]

```

if we take this to _what I believe is_ its natural conclusion (the point is to have the comma at the start of the line) you get:

```rust
let arr = [
  , 1
  , 2
  , 3
];

```

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [March 29, 2018, 1:23pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/12 "2018-03-29T13:23:44Z")

</div>

I’ve heard multiple experienced Haskell programmers say “this is a thing we do because we can’t use trailing commas”.

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [March 29, 2018, 1:26pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/13 "2018-03-29T13:26:16Z")

</div>

> [@josh](#):
>
> I’ve heard multiple experienced Haskell programmers say “this is a thing we do because we can’t use trailing commas”.

That's not my experience, but to each their own. Personally, when I write Haskell, I find it pleasant to have a leading comma for arrays.

---

<div class="post-metadata">

**Author:** ![kyrias](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kyrias/32/3872_2.png) [@kyrias](https://internals.rust-lang.org/u/kyrias)\
**Post date:** [March 29, 2018, 1:30pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/14 "2018-03-29T13:30:17Z")

</div>

I know of that style for Haskell, and you can write it the same way in current Rust. I was specifically wondering about the leading comma on the first item thing, which I’ve never seen before, thus was questioning the “some people prefer that style.”

(Additionally, it gets even weirder when the commas aren’t aligned with the square brackets.)

---

<div class="post-metadata">

**Author:** ![Tom-Phinney](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tom-phinney/32/3299_2.png) [@Tom-Phinney](https://internals.rust-lang.org/u/Tom-Phinney)\
**Post date:** [March 29, 2018, 2:35pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/15 "2018-03-29T14:35:05Z")

</div>

> [@Centril](#):
>
> In an effort to make Rust’s grammar more tolerant of macros

Somehow this discussion seems to be meandering away from its initial purpose of „macro tolerance". As humans we may prefer not to have list separators/combiners before the first element or after the last element of a list, but macros in Rust are facilitated when the language syntax tolerates them.

---

<div class="post-metadata">

**Author:** ![jmst](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jmst](https://internals.rust-lang.org/u/jmst)\
**Post date:** [March 29, 2018, 2:48pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/16 "2018-03-29T14:48:59Z")

</div>

Trailing comma on all lines instead of leading comma seems much better since it’s also an acceptable way to write normal prose, and saves a character (the space character after leading comma).

Supporting both styles would result in a mismatch of styles in the wild, which makes reading Rust code less pleasant, and catering to multiple readers seems more important than catering to the single writer’s preferences.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [March 30, 2018, 1:01pm UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/17 "2018-03-30T13:01:42Z")

</div>

> [@josh](#):
>
> I’ve heard multiple experienced Haskell programmers say “this is a thing we do because we can’t use trailing commas”.

For me, that is certainly how my use of leading comma began. But to be entirely honest, if Haskell were to one day suddenly begin supporting both trailing and leading commas, I would start writing what @centril wrote. Leading punctuation makes structure more obvious, because it lines up.

I'd probably even take leading semicolons if the world wasn't so entrenched in the current standard syntax (and were it not for the whole semicolon-equals-unit-value thing in Rust). It would certainly have eliminated the majority of those dumb edit-recompile cycles where I forgot a semicolon, or put one after a braced item. Thank goodness now for IDEs/RLS...

---

<div class="post-metadata">

**Author:** ![warlord500](https://avatars.discourse-cdn.com/v4/letter/w/4af34b/32.png) [@warlord500](https://internals.rust-lang.org/u/warlord500)\
**Post date:** [April 2, 2018, 4:11am UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/18 "2018-04-02T04:11:56Z")

</div>

I hate to be the one guy, that hates everyone ideas. but here it goes, I actually disagree with making rust more lenient syntax wise. sure makes certain code generators easier to write. but it comes with a cost and a steep one at that. by making it more lenient, It can lead to what i would call the single char cascade problem. change a single character and the entire program is effectively different. in addition a single char added some where else has absolute no effect or meaning.

I would rather have every single character to serve a purpose beyond simply stylistically pleasantness, or leniency.

It can also lead to bad or misleading error messages because their now more possible corrections. while a stricter language, has the problems effectively reduced.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/grammar-liberalizations/7170/19 "2019-03-25T08:29:59Z")

</div>

Closed on March 25, 2019, 8:29am UTC

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