Skip to content

Commit aae16d5

Browse files
chorman0773ehuss
authored andcommitted
Add identifier syntax to closure-expr, field-expr, and grouped-expr
1 parent 60b1720 commit aae16d5

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/expressions/closure-expr.md

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

3+
r[expr.closure]
4+
5+
r[expr.closure.syntax]
36
> **<sup>Syntax</sup>**\
47
> _ClosureExpression_ :\
58
> &nbsp;&nbsp; `async`[^cl-async-edition]<sup>?</sup>\
@@ -15,23 +18,40 @@
1518
>
1619
> [^cl-async-edition]: The `async` qualifier is not allowed in the 2015 edition.
1720
21+
r[expr.closure.intro]
1822
A *closure expression*, also known as a lambda expression or a lambda, defines a [closure type] and evaluates to a value of that type.
1923
The syntax for a closure expression is an optional `async` keyword, an optional `move` keyword, then a pipe-symbol-delimited (`|`) comma-separated list of [patterns], called the *closure parameters* each optionally followed by a `:` and a type, then an optional `->` and type, called the *return type*, and then an expression, called the *closure body operand*.
24+
25+
r[expr.closure.param-type]
2026
The optional type after each pattern is a type annotation for the pattern.
27+
28+
r[expr.closure.explicit-type-body]
2129
If there is a return type, the closure body must be a [block].
2230

31+
r[expr.closure.parameter-restriction]
2332
A closure expression denotes a function that maps a list of parameters onto the expression that follows the parameters.
2433
Just like a [`let` binding], the closure parameters are irrefutable [patterns], whose type annotation is optional and will be inferred from context if not given.
34+
35+
r[expr.closure.unique-type]
2536
Each closure expression has a unique, anonymous type.
2637

38+
r[expr.closure.captures]
2739
Significantly, closure expressions _capture their environment_, which regular [function definitions] do not.
40+
41+
r[expr.closure.capture-inference]
2842
Without the `move` keyword, the closure expression [infers how it captures each variable from its environment](../types/closure.md#capture-modes), preferring to capture by shared reference, effectively borrowing all outer variables mentioned inside the closure's body.
43+
44+
r[expr.closure.capture-mut-ref]
2945
If needed the compiler will infer that instead mutable references should be taken, or that the values should be moved or copied (depending on their type) from the environment.
46+
47+
r[expr.closure.capture-move]
3048
A closure can be forced to capture its environment by copying or moving values by prefixing it with the `move` keyword.
3149
This is often used to ensure that the closure's lifetime is `'static`.
3250

3351
## Closure trait implementations
3452

53+
r[expr.closure.trait-impl]
54+
3555
Which traits the closure type implement depends on how variables are captured, the types of the captured variables, and the presence of `async`.
3656
See the [call traits and coercions] chapter for how and when a closure implements `Fn`, `FnMut`, and `FnOnce`.
3757
The closure type implements [`Send`] and [`Sync`] if the type of every captured variable also implements the trait.
@@ -79,6 +99,8 @@ ten_times(move |j| println!("{}, {}", word, j));
7999

80100
## Attributes on closure parameters
81101

102+
r[expr.closure.param-attributes]
103+
82104
Attributes on closure parameters follow the same rules and restrictions as [regular function parameters].
83105

84106
[_Expression_]: ../expressions.md

src/expressions/field-expr.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
# Field access expressions
22

3+
r[expr.field]
4+
5+
r[expr.field.syntax]
36
> **<sup>Syntax</sup>**\
47
> _FieldExpression_ :\
58
> &nbsp;&nbsp; [_Expression_] `.` [IDENTIFIER]
69
10+
r[expr.field.intro]
711
A *field expression* is a [place expression] that evaluates to the location of a field of a [struct] or [union].
12+
13+
r[expr.field.mut]
814
When the operand is [mutable], the field expression is also mutable.
915

16+
r[expr.field.form]
1017
The syntax for a field expression is an expression, called the *container operand*, then a `.`, and finally an [identifier].
18+
19+
r[expr.field.constraint]
1120
Field expressions cannot be followed by a parenthetical comma-separated list of expressions, as that is instead parsed as a [method call expression].
1221
That is, they cannot be the function operand of a [call expression].
1322

@@ -36,11 +45,15 @@ foo().x;
3645
3746
## Automatic dereferencing
3847

48+
r[expr.field.autoref-deref]
49+
3950
If the type of the container operand implements [`Deref`] or [`DerefMut`][`Deref`] depending on whether the operand is [mutable], it is *automatically dereferenced* as many times as necessary to make the field access possible.
4051
This process is also called *autoderef* for short.
4152

4253
## Borrowing
4354

55+
r[expr.field.borrow]
56+
4457
The fields of a struct or a reference to a struct are treated as separate entities when borrowing.
4558
If the struct does not implement [`Drop`] and is stored in a local variable, this also applies to moving out of each of its fields.
4659
This also does not apply if automatic dereferencing is done though user-defined types other than [`Box`].

src/expressions/grouped-expr.md

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

3+
r[expr.paren]
4+
5+
r[expr.paren.syntax]
36
> **<sup>Syntax</sup>**\
47
> _GroupedExpression_ :\
58
> &nbsp;&nbsp; `(` [_Expression_] `)`
69
10+
r[expr.paren.intro]
711
A *parenthesized expression* wraps a single expression, evaluating to that expression.
812
The syntax for a parenthesized expression is a `(`, then an expression, called the *enclosed operand*, and then a `)`.
913

14+
r[expr.paren.evaluation]
1015
Parenthesized expressions evaluate to the value of the enclosed operand.
16+
17+
r[expr.paren.place-or-value]
1118
Unlike other expressions, parenthesized expressions are both [place expressions and value expressions][place].
1219
When the enclosed operand is a place expression, it is a place expression and when the enclosed operand is a value expression, it is a value expression.
1320

21+
r[expr.paren.overridew-precedence]
1422
Parentheses can be used to explicitly modify the precedence order of subexpressions within an expression.
1523

1624
An example of a parenthesized expression:

0 commit comments

Comments
 (0)