# Error message when definining enum using struct syntax

**URL:** <https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669>\
**Category:** language design\
**Created:** [November 2, 2022, 4:03am UTC](https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669 "2022-11-02T04:03:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdfriese](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rdfriese/32/10023_2.png) [@rdfriese](https://internals.rust-lang.org/u/rdfriese)\
**Post date:** [November 2, 2022, 4:03am UTC](https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669/1 "2022-11-02T04:03:47Z")

</div>

Although I know enums take to form of:

```rust
enum VecOrMap{
    Vec(Vec<usize>),
    Map(HashMap<String,usize>)
}

```

I sometimes find myself accidentally defining them as I would a struct:

```rust
enum VecOrMap{
    vec: Vec<usize>,
    map: HashMap<String,usize>
}

```

in these cases, the error of

> expected one of `(`, `,`, `=`, `{`, or `}`, found `:`

always takes more than a few seconds to parse and then realize my mistake. Is it possible to change this to something like:

> Looks like you are trying to define and enum using struct syntax, change `vec: Vec\<usize\>` to `Vec(Vec\<usize\>)`

Hopefully this type of feedback is useful!

edit: formatting

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [November 2, 2022, 4:13am UTC](https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669/2 "2022-11-02T04:13:16Z")

</div>

This is definitely a form which the compiler could recognize to provide a better error for. Please open a GitHub issue — there's a template and category specifically for error message quality that this would fit perfectly in 🙂

---

<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:** [November 2, 2022, 5:52pm UTC](https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669/3 "2022-11-02T17:52:06Z")

</div>

Definitely file bugs when you hit things like this.

Personally, I think that whenever there's an "expected one of" message without a `help:` message, it's a good bug, as a great place for a quick syntax reminder. Sketch:

```nohighlight
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
 --> src/lib.rs:2:8
  |
2 | vec: Vec<usize>,
  | ^ expected one of `(`, `,`, `=`, `{`, or `}`
  |
  | help: enum variants are either `Variant(Type)` or `Variant { field: Type }`

```

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [November 2, 2022, 8:01pm UTC](https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669/4 "2022-11-02T20:01:39Z")

</div>

I wonder if instead of these fairly opaque "`error: expected <tokens>, found <token>`" messages, the parser could print some representation of the full list of productions it is trying to match, something like:

```rust
error: expected one of
    vec
    vec = <integer>
    vec(...)
    vec { ... }

```

I guess finding a good balance between user-friendliness, terseness and precision would be tricky. Full EBNF would of course be the most precise, but verbose and possibly unfamiliar to many.

---

<div class="post-metadata">

**Author:** ![rdfriese](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rdfriese/32/10023_2.png) [@rdfriese](https://internals.rust-lang.org/u/rdfriese)\
**Post date:** [November 8, 2022, 12:24am UTC](https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669/5 "2022-11-08T00:24:26Z")

</div>

Thanks all for the discussion, I'm including a link to the github issue for convenience [https://github.com/rust-lang/rust/issues/103869](https://github.com/rust-lang/rust/issues/103869)

---

<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:** [February 6, 2023, 12:25am UTC](https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669/6 "2023-02-06T00:25:04Z")

</div>

Closed on February 6, 2023, 12:25am UTC

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