Skip to content

Commit 1891178

Browse files
committed
Editorial cleanup on expressions (part 1)
* Use "the syntax of" uniformly. * Use asterisks for all defined terms. * Define more terms, espcially in the syntax section. ** Reword things so that definitions are generally first. ** I did not necessarily go with the best wording; the idea is to improve, not perfect. I still need to dedicate time to each expressions individually. * Remove usage of "the compiler" and "Rust". ** This also involved rewording. ** How to deal with closure types and closure expressions is gonna be an interesting question to solve. I avoided solving it here. * Remove non-normative information or put them in a `Note`. * A few added section headers * Move links to the bottom Note that I've left quite a few nonsensical statements alone, such as any that use "denote". They'll be treated separately. About halfway through the list of expressions and this PR is getting large. So I'm gonna cut this one here, stopping at grouped expressions in the alphabetical list.
1 parent 361367c commit 1891178

File tree

8 files changed

+130
-92
lines changed

8 files changed

+130
-92
lines changed

src/expressions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ assert_eq!(
137137
## Place Expressions and Value Expressions
138138

139139
Expressions are divided into two main categories: place expressions and
140-
value expressions. Likewise within each expression, sub-expressions may occur
140+
value expressions. Likewise within each expression, operands may occur
141141
in either place context or value context. The evaluation of an expression
142142
depends both on its own category and the context it occurs within.
143143

src/expressions/array-expr.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] ( `,` [_Expression_] )<sup>\*</sup> `,`<sup>?</sup>\
1111
> &nbsp;&nbsp; | [_Expression_] `;` [_Expression_]
1212
13-
An _[array] expression_ can be written by enclosing zero or more comma-separated expressions of uniform type in square brackets.
13+
*Array expressions* construct [arrays][array].
14+
Array expressions come in two forms.
15+
16+
The first form lists out every value in the array.
17+
The syntax for this form is a comma-separated list of operands of uniform type enclosed in square brackets.
1418
This produces an array containing each of these values in the order they are written.
1519

16-
Alternatively there can be exactly two expressions inside the brackets, separated by a semicolon.
17-
The expression after the `;` must have type `usize` and be a [constant expression], such as a [literal] or a [constant item].
20+
The syntax for the second form is two operands separated by a semicolon (`;`) enclosed in square brackets.
21+
The operand after the `;` must have type `usize` and be a [constant expression], such as a [literal] or a [constant item].
1822
`[a; b]` creates an array containing `b` copies of the value of `a`.
19-
If the expression after the semicolon has a value greater than 1 then this requires that the type of `a` is [`Copy`], or `a` must be a path to a constant item.
23+
If the operand after the semicolon has a value greater than 1 then this requires that the type of `a` is [`Copy`], or `a` must be a path to a constant item.
2024

21-
When the repeat expression `a` is a constant item, it is evaluated `b` times.
25+
When the repeat expression, `a`, is a constant item, it is evaluated `b` times.
2226
If `b` is 0, the constant item is not evaluated at all.
2327
For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied `b` times.
2428

@@ -49,7 +53,7 @@ const EMPTY: Vec<i32> = Vec::new();
4953
> _IndexExpression_ :\
5054
> &nbsp;&nbsp; [_Expression_] `[` [_Expression_] `]`
5155
52-
[Array] and [slice]-typed expressions can be indexed by writing a square-bracket-enclosed expression of type `usize` (the index) after them.
56+
[Array] and [slice]-typed values can be indexed by writing a square-bracket-enclosed expression of type `usize` (the index) after them.
5357
When the array is mutable, the resulting [memory location] can be assigned to.
5458

5559
For other types an index expression `a[b]` is equivalent to `*std::ops::Index::index(&a, b)`, or `*std::ops::IndexMut::index_mut(&mut a, b)` in a mutable place expression context.

src/expressions/await-expr.md

+14-16
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
> _AwaitExpression_ :\
55
> &nbsp;&nbsp; [_Expression_] `.` `await`
66
7+
*Await expressions* suspend the current computation until the given future is ready to produce a value.
8+
The syntax for an await expression is an operand with a type that implements the Future trait, `.`, and then the `await` keyword.
79
Await expressions are legal only within an [async context], like an [`async fn`] or an [`async` block].
8-
They operate on a [future].
9-
Their effect is to suspend the current computation until the given future is ready to produce a value.
1010

1111
More specifically, an `<expr>.await` expression has the following effect.
1212

@@ -16,29 +16,16 @@ More specifically, an `<expr>.await` expression has the following effect.
1616
3. If the call to `poll` returns [`Poll::Pending`], then the future returns `Poll::Pending`, suspending its state so that, when the surrounding async context is re-polled,execution returns to step 2;
1717
4. Otherwise the call to `poll` must have returned [`Poll::Ready`], in which case the value contained in the [`Poll::Ready`] variant is used as the result of the `await` expression itself.
1818

19-
[`async fn`]: ../items/functions.md#async-functions
20-
[`async` block]: block-expr.md#async-blocks
21-
[future]: ../../std/future/trait.Future.html
22-
[_Expression_]: ../expressions.md
23-
[`Future::poll`]: ../../std/future/trait.Future.html#tymethod.poll
24-
[`Context`]: ../../std/task/struct.Context.html
25-
[`Pin::new_unchecked`]: ../../std/pin/struct.Pin.html#method.new_unchecked
26-
[`Poll::Pending`]: ../../std/task/enum.Poll.html#variant.Pending
27-
[`Poll::Ready`]: ../../std/task/enum.Poll.html#variant.Ready
28-
2919
> **Edition differences**: Await expressions are only available beginning with Rust 2018.
3020
3121
## Task context
3222

3323
The task context refers to the [`Context`] which was supplied to the current [async context] when the async context itself was polled.
3424
Because `await` expressions are only legal in an async context, there must be some task context available.
3525

36-
[`Context`]: ../../std/task/struct.Context.html
37-
[async context]: ../expressions/block-expr.md#async-context
38-
3926
## Approximate desugaring
4027

41-
Effectively, an `<expr>.await` expression is roughly equivalent to the following (this desugaring is not normative):
28+
Effectively, an `<expr>.await` expression is roughly equivalent to the following non-normative desugaring:
4229

4330
<!-- ignore: example expansion -->
4431
```rust,ignore
@@ -55,3 +42,14 @@ match /* <expr> */ {
5542

5643
where the `yield` pseudo-code returns `Poll::Pending` and, when re-invoked, resumes execution from that point.
5744
The variable `current_context` refers to the context taken from the async environment.
45+
46+
[_Expression_]: ../expressions.md
47+
[`async fn`]: ../items/functions.md#async-functions
48+
[`async` block]: block-expr.md#async-blocks
49+
[`context`]: ../../std/task/struct.Context.html
50+
[`future::poll`]: ../../std/future/trait.Future.html#tymethod.poll
51+
[`pin::new_unchecked`]: ../../std/pin/struct.Pin.html#method.new_unchecked
52+
[`poll::Pending`]: ../../std/task/enum.Poll.html#variant.Pending
53+
[`poll::Ready`]: ../../std/task/enum.Poll.html#variant.Ready
54+
[async context]: ../expressions/block-expr.md#async-context
55+
[future]: ../../std/future/trait.Future.html

src/expressions/block-expr.md

+35-34
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ A *block expression*, or *block*, is a control flow expression and anonymous nam
1616
As a control flow expression, a block sequentially executes its component non-item declaration statements and then its final optional expression.
1717
As an anonymous namespace scope, item declarations are only in scope inside the block itself and variables declared by `let` statements are in scope from the next statement until the end of the block.
1818

19-
Blocks are written as `{`, then any [inner attributes], then [statements], then an optional expression, and finally a `}`.
20-
Statements are usually required to be followed by a semicolon, with two exceptions.
21-
Item declaration statements do not need to be followed by a semicolon.
22-
Expression statements usually require a following semicolon except if its outer expression is a flow control expression.
19+
The syntax for a block is `{`, then any [inner attributes], then [statements], then an optional operand, and finally a `}`.
20+
21+
Statements are usually required to be followed by a semicolon, with two exceptions:
22+
23+
1. Item declaration statements do not need to be followed by a semicolon.
24+
2. Expression statements usually require a following semicolon except if its outer expression is a flow control expression.
2325
Furthermore, extra semicolons between statements are allowed, but these semicolons do not affect semantics.
2426

2527
When evaluating a block expression, each statement, except for item declaration statements, is executed sequentially.
@@ -43,36 +45,38 @@ assert_eq!(5, five);
4345

4446
> Note: As a control flow expression, if a block expression is the outer expression of an expression statement, the expected type is `()` unless it is followed immediately by a semicolon.
4547
46-
Blocks are always [value expressions] and evaluate the last expression in value expression context.
47-
This can be used to force moving a value if really needed.
48-
For example, the following example fails on the call to `consume_self` because the struct was moved out of `s` in the block expression.
48+
Blocks are always [value expressions] and evaluate the last operand in value expression context.
4949

50-
```rust,compile_fail
51-
struct Struct;
52-
53-
impl Struct {
54-
fn consume_self(self) {}
55-
fn borrow_self(&self) {}
56-
}
5750

58-
fn move_by_block_expression() {
59-
let s = Struct;
60-
61-
// Move the value out of `s` in the block expression.
62-
(&{ s }).borrow_self();
63-
64-
// Fails to execute because `s` is moved out of.
65-
s.consume_self();
66-
}
67-
```
51+
> **Note**: This can be used to force moving a value if really needed.
52+
> For example, the following example fails on the call to `consume_self` because the struct was moved out of `s` in the block expression.
53+
>
54+
> ```rust,compile_fail
55+
> struct Struct;
56+
>
57+
> impl Struct {
58+
> fn consume_self(self) {}
59+
> fn borrow_self(&self) {}
60+
> }
61+
>
62+
> fn move_by_block_expression() {
63+
> let s = Struct;
64+
>
65+
> // Move the value out of `s` in the block expression.
66+
> (&{ s }).borrow_self();
67+
>
68+
> // Fails to execute because `s` is moved out of.
69+
> s.consume_self();
70+
> }
71+
> ```
6872
6973
## `async` blocks
7074
7175
> **<sup>Syntax</sup>**\
7276
> _AsyncBlockExpression_ :\
7377
> &nbsp;&nbsp; `async` `move`<sup>?</sup> _BlockExpression_
7478
75-
An *async block* is a variant of a block expression which evaluates to a *future*.
79+
An *async block* is a variant of a block expression which evaluates to a future.
7680
The final expression of the block, if present, determines the result value of the future.
7781
7882
Executing an async block is similar to executing a closure expression:
@@ -84,26 +88,17 @@ The actual data format for this type is unspecified.
8488
8589
> **Edition differences**: Async blocks are only available beginning with Rust 2018.
8690
87-
[`std::ops::Fn`]: ../../std/ops/trait.Fn.html
88-
[`std::future::Future`]: ../../std/future/trait.Future.html
89-
9091
### Capture modes
9192
9293
Async blocks capture variables from their environment using the same [capture modes] as closures.
9394
Like closures, when written `async { .. }` the capture mode for each variable will be inferred from the content of the block.
9495
`async move { .. }` blocks however will move all referenced variables into the resulting future.
9596
96-
[capture modes]: ../types/closure.md#capture-modes
97-
[shared references]: ../types/pointer.md#shared-references-
98-
[mutable reference]: ../types/pointer.md#mutables-references-
99-
10097
### Async context
10198
10299
Because async blocks construct a future, they define an **async context** which can in turn contain [`await` expressions].
103100
Async contexts are established by async blocks as well as the bodies of async functions, whose semantics are defined in terms of async blocks.
104101
105-
[`await` expressions]: await-expr.md
106-
107102
### Control-flow operators
108103
109104
Async blocks act like a function boundary, much like closures.
@@ -171,16 +166,22 @@ fn is_unix_platform() -> bool {
171166
[_ExpressionWithoutBlock_]: ../expressions.md
172167
[_InnerAttribute_]: ../attributes.md
173168
[_Statement_]: ../statements.md
169+
[`await` expressions]: await-expr.md
174170
[`cfg`]: ../conditional-compilation.md
175171
[`for`]: loop-expr.md#iterator-loops
176172
[`loop`]: loop-expr.md#infinite-loops
173+
[`std::ops::Fn`]: ../../std/ops/trait.Fn.html
174+
[`std::future::Future`]: ../../std/future/trait.Future.html
177175
[`while let`]: loop-expr.md#predicate-pattern-loops
178176
[`while`]: loop-expr.md#predicate-loops
179177
[array expressions]: array-expr.md
180178
[call expressions]: call-expr.md
179+
[capture modes]: ../types/closure.md#capture-modes
181180
[function]: ../items/functions.md
182181
[inner attributes]: ../attributes.md
183182
[method]: ../items/associated-items.md#methods
183+
[mutable reference]: ../types/pointer.md#mutables-references-
184+
[shared references]: ../types/pointer.md#shared-references-
184185
[statement]: ../statements.md
185186
[statements]: ../statements.md
186187
[struct]: struct-expr.md

src/expressions/call-expr.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
> _CallParams_ :\
88
> &nbsp;&nbsp; [_Expression_]&nbsp;( `,` [_Expression_] )<sup>\*</sup> `,`<sup>?</sup>
99
10-
A _call expression_ consists of an expression followed by a parenthesized expression-list.
11-
It invokes a function, providing zero or more input variables.
10+
A *call expression* calls a function.
11+
The syntax of a call expression is an operand, the *function operand*, followed by a parenthesized comma-separated list of operands, the *argument operands*.
1212
If the function eventually returns, then the expression completes.
1313
For [non-function types](../types/function-item.md), the expression f(...) uses the method on one of the [`std::ops::Fn`], [`std::ops::FnMut`] or [`std::ops::FnOnce`] traits, which differ in whether they take the type by reference, mutable reference, or take ownership respectively.
1414
An automatic borrow will be taken if needed.
15-
Rust will also automatically dereference `f` as required.
15+
`f` will also be automatically dereferences as required.
16+
1617
Some examples of call expressions:
1718

1819
```rust
@@ -23,13 +24,13 @@ let name: &'static str = (|| "Rust")();
2324

2425
## Disambiguating Function Calls
2526

26-
Rust treats all function calls as sugar for a more explicit, [fully-qualified syntax].
27+
All function calls are sugar for a more explicit [fully-qualified syntax].
2728
Upon compilation, Rust will desugar all function calls into the explicit form.
2829
Rust may sometimes require you to qualify function calls with trait, depending on the ambiguity of a call in light of in-scope items.
2930

30-
> **Note**: In the past, the Rust community used the terms "Unambiguous Function Call Syntax", "Universal Function Call Syntax", or "UFCS", in documentation, issues, RFCs, and other community writings.
31-
> However, the term lacks descriptive power and potentially confuses the issue at hand.
32-
> We mention it here for searchability's sake.
31+
> **Note**: In the past, the terms "Unambiguous Function Call Syntax", "Universal Function Call Syntax", or "UFCS", have been used in documentation, issues, RFCs, and other community writings.
32+
> However, these terms lack descriptive power and potentially confuse the issue at hand.
33+
> We mention them here for searchability's sake.
3334
3435
Several situations often occur which result in ambiguities about the receiver or referent of method or associated function calls.
3536
These situations may include:

src/expressions/closure-expr.md

+21-17
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,28 @@
1212
> _ClosureParam_ :\
1313
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_Pattern_]&nbsp;( `:` [_Type_] )<sup>?</sup>
1414
15-
A _closure expression_, also know as a lambda expression or a lambda, defines a closure and denotes it as a value, in a single expression.
16-
A closure expression is a pipe-symbol-delimited (`|`) list of irrefutable [patterns] followed by an expression.
17-
Type annotations may optionally be added for the type of the parameters or for the return type.
18-
If there is a return type, the expression used for the body of the closure must be a normal [block].
19-
A closure expression also may begin with the `move` keyword before the initial `|`.
15+
A *closure expression*, also know as a lambda expression or a lambda, defines a [closure type] and evaluates to a value of that type.
16+
The syntax for a closure expression is an optional `move` keyword, then a pipe-symbol-delimited (`|`) comma-separated list of [patterns], the *closure parameters* each optionally followed by a `:` and a type, then an optional `->` and type, the *return type*, and then an operand, the *closure body*.
17+
The optional type after each pattern is a type annotation for the pattern.
18+
If there is a return type, the closure body must be a [block].
2019

2120
A closure expression denotes a function that maps a list of parameters onto the expression that follows the parameters.
22-
Just like a [`let` binding], the parameters are irrefutable [patterns], whose type annotation is optional and will be inferred from context if not given.
21+
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.
2322
Each closure expression has a unique, anonymous type.
2423

25-
Closure expressions are most useful when passing functions as arguments to other functions, as an abbreviation for defining and capturing a separate function.
26-
2724
Significantly, closure expressions _capture their environment_, which regular [function definitions] do not.
2825
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.
2926
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.
3027
A closure can be forced to capture its environment by copying or moving values by prefixing it with the `move` keyword.
31-
This is often used to ensure that the closure's type is `'static`.
28+
This is often used to ensure that the closure's lifetime is `'static`.
29+
30+
## Closure trait implementations
31+
32+
Which traits the closure type implement depends on how variables are captured and the types of the captured variables.
33+
See the [call traits and coercions] chapter for how and when a closure implements `Fn`, `FnMut`, and `FnOnce`.
34+
The closure type implements [`Send`] and [`Sync`] if the type of every captured variable also implements the trait.
3235

33-
The compiler will determine which of the [closure traits](../types/closure.md#call-traits-and-coercions) the closure's type will implement by how it acts on its captured variables.
34-
The closure will also implement [`Send`](../special-types-and-traits.md#send) and/or [`Sync`](../special-types-and-traits.md#sync) if all of its captured types do.
35-
These traits allow functions to accept closures using generics, even though the exact types can't be named.
36+
## Example
3637

3738
In this example, we define a function `ten_times` that takes a higher-order function argument, and we then call it with a closure expression as an argument, followed by a closure expression that moves values from its environment.
3839

@@ -55,15 +56,18 @@ ten_times(move |j| println!("{}, {}", word, j));
5556

5657
Attributes on closure parameters follow the same rules and restrictions as [regular function parameters].
5758

58-
[block]: block-expr.md
59-
[function definitions]: ../items/functions.md
60-
[patterns]: ../patterns.md
61-
[regular function parameters]: ../items/functions.md#attributes-on-function-parameters
62-
6359
[_Expression_]: ../expressions.md
6460
[_BlockExpression_]: block-expr.md
6561
[_TypeNoBounds_]: ../types.md#type-expressions
6662
[_Pattern_]: ../patterns.md
6763
[_Type_]: ../types.md#type-expressions
6864
[`let` binding]: ../statements.md#let-statements
65+
[`Send`]: ../special-types-and-traits.md#send
66+
[`Sync`]: ../special-types-and-traits.md#sync
6967
[_OuterAttribute_]: ../attributes.md
68+
[block]: block-expr.md
69+
[call traits and coercions]: ../types/closure.md#call-traits-and-coercions
70+
[closure type]: ../types/closure.md
71+
[function definitions]: ../items/functions.md
72+
[patterns]: ../patterns.md
73+
[regular function parameters]: ../items/functions.md#attributes-on-function-parameters

0 commit comments

Comments
 (0)