Skip to content

Commit 54ed666

Browse files
chorman0773ehuss
authored andcommitted
Add identifier syntax to path-expr, range-expr, return-expr, struct-expr, tuple-expr, and underscore-expr
1 parent 735991b commit 54ed666

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

src/expressions/path-expr.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# Path expressions
22

3+
r[expr.path]
4+
5+
r[expr.path.syntax]
36
> **<sup>Syntax</sup>**\
47
> _PathExpression_ :\
58
> &nbsp;&nbsp; &nbsp;&nbsp; [_PathInExpression_]\
69
> &nbsp;&nbsp; | [_QualifiedPathInExpression_]
710
11+
r[expr.path.intro]
812
A [path] used as an expression context denotes either a local variable or an item.
13+
14+
r[expr.path.place]
915
Path expressions that resolve to local or static variables are [place expressions], other paths are [value expressions].
16+
17+
r[expr.path.safety]
1018
Using a [`static mut`] variable requires an [`unsafe` block].
1119

1220
```rust
@@ -23,6 +31,7 @@ let push_integer = Vec::<i32>::push;
2331
let slice_reverse = <[i32]>::reverse;
2432
```
2533

34+
r[expr.path.const]
2635
Evaluation of associated constants is handled the same way as [`const` blocks].
2736

2837
[_PathInExpression_]: ../paths.md#paths-in-expressions

src/expressions/range-expr.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Range expressions
22

3+
r[expr.range]
4+
5+
r[expr.range.syntax]
36
> **<sup>Syntax</sup>**\
47
> _RangeExpression_ :\
58
> &nbsp;&nbsp; &nbsp;&nbsp; _RangeExpr_\
@@ -27,6 +30,7 @@
2730
> _RangeToInclusiveExpr_ :\
2831
> &nbsp;&nbsp; `..=` [_Expression_]
2932
33+
r[expr.range.behaviour]
3034
The `..` and `..=` operators will construct an object of one of the `std::ops::Range` (or `core::ops::Range`) variants, according to the following table:
3135

3236
| Production | Syntax | Type | Range |
@@ -49,6 +53,7 @@ Examples:
4953
..=7; // std::ops::RangeToInclusive
5054
```
5155

56+
r[expr.range.equivalence]
5257
The following expressions are equivalent.
5358

5459
```rust
@@ -58,6 +63,7 @@ let y = 0..10;
5863
assert_eq!(x, y);
5964
```
6065

66+
r[expr.range.for]
6167
Ranges can be used in `for` loops:
6268

6369
```rust

src/expressions/return-expr.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# `return` expressions
22

3+
r[expr.return]
4+
5+
r[expr.return.syntax]
36
> **<sup>Syntax</sup>**\
47
> _ReturnExpression_ :\
58
> &nbsp;&nbsp; `return` [_Expression_]<sup>?</sup>
69
10+
r[expr.return.intro]
711
Return expressions are denoted with the keyword `return`.
12+
13+
r[expr.return.behaviour]
814
Evaluating a `return` expression moves its argument into the designated output location for the current function call, destroys the current function activation frame, and transfers control to the caller frame.
915

1016
An example of a `return` expression:

src/expressions/struct-expr.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Struct expressions
22

3+
r[expr.struct]
4+
5+
r[expr.struct.syntax]
36
> **<sup>Syntax</sup>**\
47
> _StructExpression_ :\
58
> &nbsp;&nbsp; &nbsp;&nbsp; _StructExprStruct_\
@@ -29,6 +32,7 @@
2932
>
3033
> _StructExprUnit_ : [_PathInExpression_]
3134
35+
r[expr.struct.intro]
3236
A *struct expression* creates a struct, enum, or union value.
3337
It consists of a path to a [struct], [enum variant], or [union] item followed by the values for the fields of the item.
3438
There are three forms of struct expressions: struct, tuple, and unit.
@@ -51,17 +55,29 @@ some_fn::<Cookie>(Cookie);
5155

5256
## Field struct expression
5357

58+
r[expr.struct.field]
59+
60+
r[expr.struct.field.intro]
5461
A struct expression with fields enclosed in curly braces allows you to specify the value for each individual field in any order.
5562
The field name is separated from its value with a colon.
5663

64+
r[expr.struct.field.union-constraint]
5765
A value of a [union] type can only be created using this syntax, and it must specify exactly one field.
5866

5967
## Functional update syntax
6068

69+
r[expr.struct.update]
70+
71+
r[expr.struct.update.intro]
6172
A struct expression that constructs a value of a struct type can terminate with the syntax `..` followed by an expression to denote a functional update.
73+
74+
r[expr.struct.update.constraint]
6275
The expression following `..` (the base) must have the same struct type as the new struct type being formed.
6376

77+
r[expr.struct.update.fields]
6478
The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
79+
80+
r[expr.struct.update.visibility-constraint]
6581
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
6682

6783
```rust
@@ -72,9 +88,11 @@ Point3d {y: 0, z: 10, .. base}; // OK, only base.x is accessed
7288
drop(y_ref);
7389
```
7490

91+
r[expr.struct.restriction]
7592
Struct expressions with curly braces can't be used directly in a [loop] or [if] expression's head, or in the [scrutinee] of an [if let] or [match] expression.
7693
However, struct expressions can be used in these situations if they are within another expression, for example inside [parentheses].
7794

95+
r[expr.struct.tuple-field]
7896
The field names can be decimal integer values to specify indices for constructing tuple structs.
7997
This can be used with base structs to fill out the remaining indices not specified:
8098

@@ -87,6 +105,8 @@ let c3 = Color{1: 0, ..c2}; // Fill out all other fields using a base struct.
87105

88106
### Struct field init shorthand
89107

108+
r[expr.struct.field.named]
109+
90110
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`.
91111
This allows a compact syntax with less duplication.
92112
For example:
@@ -102,6 +122,8 @@ Point3d { x, y: y_value, z };
102122

103123
## Tuple struct expression
104124

125+
r[expr.struct.tuple]
126+
105127
A struct expression with fields enclosed in parentheses constructs a tuple struct.
106128
Though it is listed here as a specific expression for completeness, it is equivalent to a [call expression] to the tuple struct's constructor. For example:
107129

@@ -114,6 +136,8 @@ let pos = c(8, 6, 7); // Creates a `Position` value.
114136

115137
## Unit struct expression
116138

139+
r[expr.struct.unit]
140+
117141
A unit struct expression is just the path to a unit struct item.
118142
This refers to the unit struct's implicit constant of its value.
119143
The unit struct value can also be constructed with a fieldless struct expression. For example:

src/expressions/tuple-expr.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
# Tuple and tuple indexing expressions
22

3+
r[expr.tuple]
4+
35
## Tuple expressions
46

7+
r[expr.tuple]
8+
9+
r[expr.tuple.syntax]
510
> **<sup>Syntax</sup>**\
611
> _TupleExpression_ :\
712
> &nbsp;&nbsp; `(` _TupleElements_<sup>?</sup> `)`
813
>
914
> _TupleElements_ :\
1015
> &nbsp;&nbsp; ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup>
1116
17+
r[expr.tuple.result]
1218
A *tuple expression* constructs [tuple values][tuple type].
1319

20+
r[expr.tuple.intro]
1421
The syntax for tuple expressions is a parenthesized, comma separated list of expressions, called the *tuple initializer operands*.
22+
23+
r[expr.tuple.unary-tuple-restriction]
1524
1-ary tuple expressions require a comma after their tuple initializer operand to be disambiguated with a [parenthetical expression].
1625

26+
r[expr.tuple.value]
1727
Tuple expressions are a [value expression] that evaluate into a newly constructed value of a tuple type.
28+
29+
r[expr.tuple.type]
1830
The number of tuple initializer operands is the arity of the constructed tuple.
31+
32+
r[expr.tuple.unit]
1933
Tuple expressions without any tuple initializer operands produce the unit tuple.
34+
35+
r[expr.tuple.fields]
2036
For other tuple expressions, the first written tuple initializer operand initializes the field `0` and subsequent operands initializes the next highest field.
2137
For example, in the tuple expression `('a', 'b', 'c')`, `'a'` initializes the value of the field `0`, `'b'` field `1`, and `'c'` field `2`.
2238

@@ -31,19 +47,27 @@ Examples of tuple expressions and their types:
3147

3248
## Tuple indexing expressions
3349

50+
r[expr.tuple-index]
51+
52+
r[expr.tuple-index.syntax]
3453
> **<sup>Syntax</sup>**\
3554
> _TupleIndexingExpression_ :\
3655
> &nbsp;&nbsp; [_Expression_] `.` [TUPLE_INDEX]
3756
57+
r[expr.tuple-index.intro]
3858
A *tuple indexing expression* accesses fields of [tuples][tuple type] and [tuple structs][tuple struct].
3959

4060
The syntax for a tuple index expression is an expression, called the *tuple operand*, then a `.`, then finally a tuple index.
61+
62+
r[expr.tuple-index.restriction]
4163
The syntax for the *tuple index* is a [decimal literal] with no leading zeros, underscores, or suffix.
4264
For example `0` and `2` are valid tuple indices but not `01`, `0_`, nor `0i32`.
4365

66+
r[expr.tuple-index.constraint]
4467
The type of the tuple operand must be a [tuple type] or a [tuple struct].
4568
The tuple index must be a name of a field of the type of the tuple operand.
4669

70+
r[expr.tuple-index.result]
4771
Evaluation of tuple index expressions has no side effects beyond evaluation of its tuple operand.
4872
As a [place expression], it evaluates to the location of the field of the tuple operand with the same name as the tuple index.
4973

src/expressions/underscore-expr.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# `_` expressions
22

3+
r[expr.placeholder]
4+
5+
r[expr.placeholder.syntax]
36
> **<sup>Syntax</sup>**\
47
> _UnderscoreExpression_ :\
58
> &nbsp;&nbsp; `_`
69
10+
r[expr.placeholder.intro]
711
Underscore expressions, denoted with the symbol `_`, are used to signify a
8-
placeholder in a destructuring assignment. They may only appear in the left-hand
9-
side of an assignment.
12+
placeholder in a destructuring assignment.
13+
14+
r[expr.placeholder.constraint]
15+
They may only appear in the left-hand side of an assignment.
1016

17+
r[expr.placeholder.pattern]
1118
Note that this is distinct from the [wildcard pattern](../patterns.md#wildcard-pattern).
1219

1320
Examples of `_` expressions:

0 commit comments

Comments
 (0)