Skip to content

Commit

Permalink
Document rfc 1506 - clarified adt kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Apr 7, 2017
1 parent 644a4ff commit cf4b323
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions src/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ comma:

## Struct expressions

There are several forms of struct expressions. A _struct expression_
consists of the [path](paths.html) of a [struct item](items.html#structs), followed
by a brace-enclosed list of zero or more comma-separated name-value pairs,
providing the field values of a new instance of the struct. A field name can be
any identifier, and is separated from its value expression by a colon. The
location denoted by a struct field is mutable if and only if the enclosing
struct is mutable.
There are several forms of struct expressions. A _struct expression_ consists
of the [path](paths.html) of a [struct item](items.html#structs), followed by a
brace-enclosed list of zero or more comma-separated name-value pairs, providing
the field values of a new instance of the struct. A field name can be any
identifier, and is separated from its value expression by a colon. In the case
of a tuple structs the field names are instead decimal integer literals,
containing no underscores and with no leading zeros or suffix, corresponding to
the position of the field. The location denoted by a struct field is mutable if
and only if the enclosing struct is mutable.

A _tuple struct expression_ consists of the [path](paths.html) of a [struct
item](items.html#structs), followed by a parenthesized list of one or more
Expand All @@ -150,6 +152,7 @@ The following are examples of struct expressions:
Point {x: 10.0, y: 20.0};
NothingInMe {};
TuplePoint(10.0, 20.0);
TuplePoint { 0: 10.0, 1: 20.0 }; // Same as above line
let u = game::User {name: "Joe", age: 35, score: 100_000};
some_fn::<Cookie>(Cookie);
```
Expand All @@ -174,9 +177,9 @@ Point3d {y: 0, z: 10, .. base};

#### Struct field init shorthand

When initializing a data structure (struct, enum, union) with named fields,
it is allowed to write `fieldname` as a shorthand for `fieldname: fieldname`.
This allows a compact syntax with less duplication.
When initializing a data structure (struct, enum, union) with named (but not
numbered) fields, it is allowed to write `fieldname` as a shorthand for
`fieldname: fieldname`. This allows a compact syntax with less duplication.

Example:

Expand Down Expand Up @@ -698,10 +701,6 @@ variable binding specifications, wildcards (`..`), and placeholders (`_`). A
the patterns. The type of the patterns must equal the type of the head
expression.

In a pattern whose head expression has an `enum` type, a placeholder (`_`)
stands for a *single* data field, whereas a wildcard `..` stands for *all* the
fields of a particular variant.

A `match` behaves differently depending on whether or not the head expression
is an [lvalue or an rvalue](expressions.html#lvalues-rvalues-and-temporaries).
If the head expression is an rvalue, it is first evaluated into a temporary
Expand Down Expand Up @@ -737,19 +736,32 @@ matched value (depending on the matched value's type). This can be changed to
bind to a reference by using the `ref` keyword, or to a mutable reference using
`ref mut`.

Subpatterns can also be bound to variables by the use of the syntax `variable @
subpattern`. For example:
Patterns can be used to *destructure* structs, enums, and tuples. Destructuring
breaks a value up into its component pieces. The syntax used is the same as
when creating such values. When destructing a data structure with named (but
not numbered) fields, it is allowed to write `fieldname` as a shorthand for
`fieldname: fieldname`. In a pattern whose head expression has a `struct`,
`enum` or `tupl` type, a placeholder (`_`) stands for a *single* data field,
whereas a wildcard `..` stands for *all* the fields of a particular variant.

```rust
let x = 1;

match x {
e @ 1 ... 5 => println!("got a range element {}", e),
_ => println!("anything"),
}
# enum Message {
# Quit,
# WriteString(String),
# Move { x: i32, y: i32 },
# ChangeColor(u8, u8, u8),
# }
# let message = Message::Quit;
match message {
Message::Quit => "Quit",
Message::WritingString(write) => &write,
Message::Move{ x, y: 3 } => "move 3",
Message::Move{ .. } => "other move",
Message::ChangeColor { 0: red, 1: green, 2: _ } => "color change",
};
```

Patterns can also dereference pointers by using the `&`, `&mut` and `box`
Patterns can also dereference pointers by using the `&` and `&mut`
symbols, as appropriate. For example, these two matches on `x: &i32` are
equivalent:

Expand All @@ -761,6 +773,18 @@ let z = match x { &0 => "zero", _ => "some" };
assert_eq!(y, z);
```

Subpatterns can also be bound to variables by the use of the syntax `variable @
subpattern`. For example:

```rust
let x = 1;

match x {
e @ 1 ... 5 => println!("got a range element {}", e),
_ => println!("anything"),
}
```

Multiple match patterns may be joined with the `|` operator. A range of values
may be specified with `...`. For example:

Expand Down

0 comments on commit cf4b323

Please sign in to comment.