Skip to content

Commit 2058862

Browse files
committed
Add identifier syntax to closure-expr, field-expr, and grouped-expr
1 parent 2714dea commit 2058862

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; `move`<sup>?</sup>\
@@ -12,23 +15,40 @@
1215
> _ClosureParam_ :\
1316
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_PatternNoTopAlt_]&nbsp;( `:` [_Type_] )<sup>?</sup>
1417
18+
r[expr.closure.intro]
1519
A *closure expression*, also known as a lambda expression or a lambda, defines a [closure type] and evaluates to a value of that type.
1620
The syntax for a closure expression is 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*.
21+
22+
r[expr.closure.param-type]
1723
The optional type after each pattern is a type annotation for the pattern.
24+
25+
r[expr.closure.explicit-type-body]
1826
If there is a return type, the closure body must be a [block].
1927

28+
r[expr.closure.parameter-restriction]
2029
A closure expression denotes a function that maps a list of parameters onto the expression that follows the parameters.
2130
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.
31+
32+
r[expr.closure.unique-type]
2233
Each closure expression has a unique, anonymous type.
2334

35+
r[expr.closure.captures]
2436
Significantly, closure expressions _capture their environment_, which regular [function definitions] do not.
37+
38+
r[expr.closure.capture-inference]
2539
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.
40+
41+
r[expr.closure.capture-mut-ref]
2642
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.
43+
44+
r[expr.closure.capture-move]
2745
A closure can be forced to capture its environment by copying or moving values by prefixing it with the `move` keyword.
2846
This is often used to ensure that the closure's lifetime is `'static`.
2947

3048
## Closure trait implementations
3149

50+
r[expr.closure.trait-impl]
51+
3252
Which traits the closure type implement depends on how variables are captured and the types of the captured variables.
3353
See the [call traits and coercions] chapter for how and when a closure implements `Fn`, `FnMut`, and `FnOnce`.
3454
The closure type implements [`Send`] and [`Sync`] if the type of every captured variable also implements the trait.
@@ -54,6 +74,8 @@ ten_times(move |j| println!("{}, {}", word, j));
5474

5575
## Attributes on closure parameters
5676

77+
r[expr.closure.param-attributes]
78+
5779
Attributes on closure parameters follow the same rules and restrictions as [regular function parameters].
5880

5981
[_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)