You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A *closure expression*, also known as a lambda expression or a lambda, defines a [closure type] and evaluates to a value of that type.
16
20
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]
17
23
The optional type after each pattern is a type annotation for the pattern.
24
+
25
+
r[expr.closure.explicit-type-body]
18
26
If there is a return type, the closure body must be a [block].
19
27
28
+
r[expr.closure.parameter-restriction]
20
29
A closure expression denotes a function that maps a list of parameters onto the expression that follows the parameters.
21
30
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]
22
33
Each closure expression has a unique, anonymous type.
23
34
35
+
r[expr.closure.captures]
24
36
Significantly, closure expressions _capture their environment_, which regular [function definitions] do not.
37
+
38
+
r[expr.closure.capture-inference]
25
39
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]
26
42
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]
27
45
A closure can be forced to capture its environment by copying or moving values by prefixing it with the `move` keyword.
28
46
This is often used to ensure that the closure's lifetime is `'static`.
29
47
30
48
## Closure trait implementations
31
49
50
+
r[expr.closure.trait-impl]
51
+
32
52
Which traits the closure type implement depends on how variables are captured and the types of the captured variables.
33
53
See the [call traits and coercions] chapter for how and when a closure implements `Fn`, `FnMut`, and `FnOnce`.
34
54
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));
54
74
55
75
## Attributes on closure parameters
56
76
77
+
r[expr.closure.param-attributes]
78
+
57
79
Attributes on closure parameters follow the same rules and restrictions as [regular function parameters].
Copy file name to clipboardExpand all lines: src/expressions/field-expr.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,22 @@
1
1
# Field access expressions
2
2
3
+
r[expr.field]
4
+
5
+
r[expr.field.syntax]
3
6
> **<sup>Syntax</sup>**\
4
7
> _FieldExpression_ :\
5
8
> [_Expression_]`.`[IDENTIFIER]
6
9
10
+
r[expr.field.intro]
7
11
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]
8
14
When the operand is [mutable], the field expression is also mutable.
9
15
16
+
r[expr.field.form]
10
17
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]
11
20
Field expressions cannot be followed by a parenthetical comma-separated list of expressions, as that is instead parsed as a [method call expression].
12
21
That is, they cannot be the function operand of a [call expression].
13
22
@@ -36,11 +45,15 @@ foo().x;
36
45
37
46
## Automatic dereferencing
38
47
48
+
r[expr.field.autoref-deref]
49
+
39
50
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.
40
51
This process is also called *autoderef* for short.
41
52
42
53
## Borrowing
43
54
55
+
r[expr.field.borrow]
56
+
44
57
The fields of a struct or a reference to a struct are treated as separate entities when borrowing.
45
58
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.
46
59
This also does not apply if automatic dereferencing is done though user-defined types other than [`Box`].
Copy file name to clipboardExpand all lines: src/expressions/grouped-expr.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,24 @@
1
1
# Grouped expressions
2
2
3
+
r[expr.paren]
4
+
5
+
r[expr.paren.syntax]
3
6
> **<sup>Syntax</sup>**\
4
7
> _GroupedExpression_ :\
5
8
> `(`[_Expression_]`)`
6
9
10
+
r[expr.paren.intro]
7
11
A *parenthesized expression* wraps a single expression, evaluating to that expression.
8
12
The syntax for a parenthesized expression is a `(`, then an expression, called the *enclosed operand*, and then a `)`.
9
13
14
+
r[expr.paren.evaluation]
10
15
Parenthesized expressions evaluate to the value of the enclosed operand.
16
+
17
+
r[expr.paren.place-or-value]
11
18
Unlike other expressions, parenthesized expressions are both [place expressions and value expressions][place].
12
19
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.
13
20
21
+
r[expr.paren.overridew-precedence]
14
22
Parentheses can be used to explicitly modify the precedence order of subexpressions within an expression.
0 commit comments