Skip to content

Grammar: Add enum expression. #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/expressions/enum-variant-expr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# Enumeration Variant expressions

Enumeration variants can be constructed similarly to structs, using a path to
an enum variant instead of to a struct:
> **<sup>Syntax</sup>**\
> _EnumerationVariantExpression_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _EnumExprStruct_\
> &nbsp;&nbsp; | _EnumExprTuple_\
> &nbsp;&nbsp; | _EnumExprFieldless_
>
> _EnumExprStruct_ :\
> &nbsp;&nbsp; [_PathInExpression_] `{` _EnumExprFields_<sup>?</sup> `}`
>
> _EnumExprFields_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; _EnumExprField_ (`,` _EnumExprField_)<sup>\*</sup> `,`<sup>?</sup>
>
> _EnumExprField_ :\
> &nbsp;&nbsp; &nbsp;&nbsp; [IDENTIFIER]\
> &nbsp;&nbsp; | ([IDENTIFIER] | [TUPLE_INDEX]) `:` [_Expression_]
>
> _EnumExprTuple_ :\
> &nbsp;&nbsp; [_PathInExpression_] `(`\
> &nbsp;&nbsp; &nbsp;&nbsp; ( [_Expression_] (`,` [_Expression_])<sup>\*</sup> `,`<sup>?</sup> )<sup>?</sup>\
> &nbsp;&nbsp; `)`
>
> _EnumExprFieldless_ : [_PathInExpression_]

Enumeration variants can be constructed similarly to [structs], using a path to an enum
variant instead of to a struct:

```rust
# enum Message {
@@ -13,3 +36,12 @@ let q = Message::Quit;
let w = Message::WriteString("Some string".to_string());
let m = Message::Move { x: 50, y: 200 };
```

Enum variant expressions have the same syntax, behavior, and restrictions as [struct
expressions][structs], except they do not support base update with the `..` syntax.

[IDENTIFIER]: identifiers.html
[TUPLE_INDEX]: tokens.html#integer-literals
[_Expression_]: expressions.html
[_PathInExpression_]: paths.html#paths-in-expressions
[structs]: expressions/struct-expr.html