Skip to content

Commit 4cbbd87

Browse files
authored
Merge pull request #962 from Havvy/one-line-expr
One sentence is one line src/expressions/*
2 parents 5037683 + eb52903 commit 4cbbd87

18 files changed

+342
-580
lines changed

src/expressions/array-expr.md

+18-32
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,22 @@
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
14-
comma-separated expressions of uniform type in square brackets. This produces
15-
an array containing each of these values in the order they are written.
13+
An _[array] expression_ can be written by enclosing zero or more comma-separated expressions of uniform type in square brackets.
14+
This produces an array containing each of these values in the order they are written.
1615

17-
Alternatively there can be exactly two expressions inside the brackets,
18-
separated by a semicolon. The expression after the `;` must have type `usize`
19-
and be a [constant expression], such as a [literal] or a [constant item]. `[a;
20-
b]` creates an array containing `b` copies of the value of `a`. If the
21-
expression after the semicolon has a value greater than 1 then this requires
22-
that the type of `a` is [`Copy`], or `a` must be a path to a constant item.
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].
18+
`[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.
2320

2421
When the repeat expression `a` is a constant item, it is evaluated `b` times.
25-
If `b` is 0, the constant item is not evaluated at all. For expressions that
26-
are not a constant item, it is evaluated exactly once, and then the result is
27-
copied `b` times.
22+
If `b` is 0, the constant item is not evaluated at all.
23+
For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied `b` times.
2824

2925
<div class="warning">
3026

31-
Warning: In the case where `b` is 0, and `a` is a non-constant item, there is
32-
currently a bug in `rustc` where the value `a` is evaluated but not dropped,
33-
thus causing a leak. See [issue
34-
#74836](https://github.com/rust-lang/rust/issues/74836).
27+
Warning: In the case where `b` is 0, and `a` is a non-constant item, there is currently a bug in `rustc` where the value `a` is evaluated but not dropped, thus causing a leak.
28+
See [issue #74836](https://github.com/rust-lang/rust/issues/74836).
3529

3630
</div>
3731

@@ -47,30 +41,23 @@ const EMPTY: Vec<i32> = Vec::new();
4741

4842
### Array expression attributes
4943

50-
[Inner attributes] are allowed directly after the opening bracket of an array
51-
expression in the same expression contexts as [attributes on block
52-
expressions].
44+
[Inner attributes] are allowed directly after the opening bracket of an array expression in the same expression contexts as [attributes on block expressions].
5345

5446
## Array and slice indexing expressions
5547

5648
> **<sup>Syntax</sup>**\
5749
> _IndexExpression_ :\
5850
> &nbsp;&nbsp; [_Expression_] `[` [_Expression_] `]`
5951
60-
[Array] and [slice]-typed expressions can be indexed by writing a
61-
square-bracket-enclosed expression of type `usize` (the index) after them.
52+
[Array] and [slice]-typed expressions can be indexed by writing a square-bracket-enclosed expression of type `usize` (the index) after them.
6253
When the array is mutable, the resulting [memory location] can be assigned to.
6354

64-
For other types an index expression `a[b]` is equivalent to
65-
`*std::ops::Index::index(&a, b)`, or
66-
`*std::ops::IndexMut::index_mut(&mut a, b)` in a mutable place expression
67-
context. Just as with methods, Rust will also insert dereference operations on
68-
`a` repeatedly to find an implementation.
55+
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.
56+
Just as with methods, Rust will also insert dereference operations on `a` repeatedly to find an implementation.
6957

70-
Indices are zero-based for arrays and slices. Array access is a [constant
71-
expression], so bounds can be checked at compile-time with a constant index
72-
value. Otherwise a check will be performed at run-time that will put the thread
73-
in a _panicked state_ if it fails.
58+
Indices are zero-based for arrays and slices.
59+
Array access is a [constant expression], so bounds can be checked at compile-time with a constant index value.
60+
Otherwise a check will be performed at run-time that will put the thread in a _panicked state_ if it fails.
7461

7562
```rust,should_panic
7663
// lint is deny by default.
@@ -90,8 +77,7 @@ let arr = ["a", "b"];
9077
arr[10]; // warning: index out of bounds
9178
```
9279

93-
The array index expression can be implemented for types other than arrays and slices
94-
by implementing the [Index] and [IndexMut] traits.
80+
The array index expression can be implemented for types other than arrays and slices by implementing the [Index] and [IndexMut] traits.
9581

9682
[`Copy`]: ../special-types-and-traits.md#copy
9783
[IndexMut]: ../../std/ops/trait.IndexMut.html

src/expressions/await-expr.md

+12-25
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44
> _AwaitExpression_ :\
55
> &nbsp;&nbsp; [_Expression_] `.` `await`
66
7-
Await expressions are legal only within an [async context], like an
8-
[`async fn`] or an [`async` block]. They operate on a [future]. Their effect
9-
is to suspend the current computation until the given future is ready
10-
to produce a value.
7+
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.
1110

1211
More specifically, an `<expr>.await` expression has the following effect.
1312

1413
1. Evaluate `<expr>` to a [future] `tmp`;
1514
2. Pin `tmp` using [`Pin::new_unchecked`];
16-
3. This pinned future is then polled by calling the [`Future::poll`] method and
17-
passing it the current [task context](#task-context);
18-
3. If the call to `poll` returns [`Poll::Pending`], then the future
19-
returns `Poll::Pending`, suspending its state so that, when the
20-
surrounding async context is re-polled, execution returns to step
21-
2;
22-
4. Otherwise the call to `poll` must have returned [`Poll::Ready`], in which case the
23-
value contained in the [`Poll::Ready`] variant is used as the result
24-
of the `await` expression itself.
15+
3. This pinned future is then polled by calling the [`Future::poll`] method and passing it the current [task context](#task-context);
16+
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;
17+
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.
2518

2619
[`async fn`]: ../items/functions.md#async-functions
2720
[`async` block]: block-expr.md#async-blocks
@@ -33,23 +26,19 @@ More specifically, an `<expr>.await` expression has the following effect.
3326
[`Poll::Pending`]: ../../std/task/enum.Poll.html#variant.Pending
3427
[`Poll::Ready`]: ../../std/task/enum.Poll.html#variant.Ready
3528

36-
> **Edition differences**: Await expressions are only available beginning with
37-
> Rust 2018.
29+
> **Edition differences**: Await expressions are only available beginning with Rust 2018.
3830
3931
## Task context
4032

41-
The task context refers to the [`Context`] which was supplied to the
42-
current [async context] when the async context itself was
43-
polled. Because `await` expressions are only legal in an async
44-
context, there must be some task context available.
33+
The task context refers to the [`Context`] which was supplied to the current [async context] when the async context itself was polled.
34+
Because `await` expressions are only legal in an async context, there must be some task context available.
4535

4636
[`Context`]: ../../std/task/struct.Context.html
4737
[async context]: ../expressions/block-expr.md#async-context
4838

4939
## Approximate desugaring
5040

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

5443
<!-- ignore: example expansion -->
5544
```rust,ignore
@@ -64,7 +53,5 @@ match /* <expr> */ {
6453
}
6554
```
6655

67-
where the `yield` pseudo-code returns `Poll::Pending` and, when
68-
re-invoked, resumes execution from that point. The variable
69-
`current_context` refers to the context taken from the async
70-
environment.
56+
where the `yield` pseudo-code returns `Poll::Pending` and, when re-invoked, resumes execution from that point.
57+
The variable `current_context` refers to the context taken from the async environment.

src/expressions/block-expr.md

+39-65
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,20 @@
1212
> &nbsp;&nbsp; | [_Statement_]<sup>\+</sup> [_ExpressionWithoutBlock_]\
1313
> &nbsp;&nbsp; | [_ExpressionWithoutBlock_]
1414
15-
A *block expression*, or *block*, is a control flow expression and anonymous
16-
namespace scope for items and variable declarations. As a control flow
17-
expression, a block sequentially executes its component non-item declaration
18-
statements and then its final optional expression. As an anonymous namespace
19-
scope, item declarations are only in scope inside the block itself and variables
20-
declared by `let` statements are in scope from the next statement until the end
21-
of the block.
22-
23-
Blocks are written as `{`, then any [inner attributes], then [statements],
24-
then an optional expression, and finally a `}`. Statements are usually required
25-
to be followed by a semicolon, with two exceptions. Item declaration statements do
26-
not need to be followed by a semicolon. Expression statements usually require
27-
a following semicolon except if its outer expression is a flow control
28-
expression. Furthermore, extra semicolons between statements are allowed, but
29-
these semicolons do not affect semantics.
30-
31-
When evaluating a block expression, each statement, except for item declaration
32-
statements, is executed sequentially. Then the final expression is executed,
33-
if given.
34-
35-
The type of a block is the type of the final expression, or `()` if the final
36-
expression is omitted.
15+
A *block expression*, or *block*, is a control flow expression and anonymous namespace scope for items and variable declarations.
16+
As a control flow expression, a block sequentially executes its component non-item declaration statements and then its final optional expression.
17+
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.
18+
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.
23+
Furthermore, extra semicolons between statements are allowed, but these semicolons do not affect semantics.
24+
25+
When evaluating a block expression, each statement, except for item declaration statements, is executed sequentially.
26+
Then the final expression is executed, if given.
27+
28+
The type of a block is the type of the final expression, or `()` if the final expression is omitted.
3729

3830
```rust
3931
# fn fn_call() {}
@@ -49,14 +41,11 @@ let five: i32 = {
4941
assert_eq!(5, five);
5042
```
5143

52-
> Note: As a control flow expression, if a block expression is the outer
53-
> expression of an expression statement, the expected type is `()` unless it
54-
> is followed immediately by a semicolon.
44+
> 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.
5545
56-
Blocks are always [value expressions] and evaluate the last expression in
57-
value expression context. This can be used to force moving a value if really
58-
needed. For example, the following example fails on the call to `consume_self`
59-
because the struct was moved out of `s` in the block expression.
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.
6049

6150
```rust,compile_fail
6251
struct Struct;
@@ -83,20 +72,15 @@ fn move_by_block_expression() {
8372
> _AsyncBlockExpression_ :\
8473
> &nbsp;&nbsp; `async` `move`<sup>?</sup> _BlockExpression_
8574
86-
An *async block* is a variant of a block expression which evaluates to
87-
a *future*. The final expression of the block, if present, determines
88-
the result value of the future.
75+
An *async block* is a variant of a block expression which evaluates to a *future*.
76+
The final expression of the block, if present, determines the result value of the future.
8977

9078
Executing an async block is similar to executing a closure expression:
9179
its immediate effect is to produce and return an anonymous type.
92-
Whereas closures return a type that implements one or more of the
93-
[`std::ops::Fn`] traits, however, the type returned for an async block
94-
implements the [`std::future::Future`] trait. The actual data format for
95-
this type is unspecified.
80+
Whereas closures return a type that implements one or more of the [`std::ops::Fn`] traits, however, the type returned for an async block implements the [`std::future::Future`] trait.
81+
The actual data format for this type is unspecified.
9682

97-
> **Note:** The future type that rustc generates is roughly equivalent
98-
> to an enum with one variant per `await` point, where each variant
99-
> stores the data needed to resume from its corresponding point.
83+
> **Note:** The future type that rustc generates is roughly equivalent to an enum with one variant per `await` point, where each variant stores the data needed to resume from its corresponding point.
10084
10185
> **Edition differences**: Async blocks are only available beginning with Rust 2018.
10286
@@ -105,37 +89,30 @@ this type is unspecified.
10589

10690
### Capture modes
10791

108-
Async blocks capture variables from their environment using the same
109-
[capture modes] as closures. Like closures, when written `async {
110-
.. }` the capture mode for each variable will be inferred from the
111-
content of the block. `async move { .. }` blocks however will move all
112-
referenced variables into the resulting future.
92+
Async blocks capture variables from their environment using the same [capture modes] as closures.
93+
Like closures, when written `async { .. }` the capture mode for each variable will be inferred from the content of the block.
94+
`async move { .. }` blocks however will move all referenced variables into the resulting future.
11395

11496
[capture modes]: ../types/closure.md#capture-modes
11597
[shared references]: ../types/pointer.md#shared-references-
11698
[mutable reference]: ../types/pointer.md#mutables-references-
11799

118100
### Async context
119101

120-
Because async blocks construct a future, they define an **async
121-
context** which can in turn contain [`await` expressions]. Async
122-
contexts are established by async blocks as well as the bodies of
123-
async functions, whose semantics are defined in terms of async blocks.
102+
Because async blocks construct a future, they define an **async context** which can in turn contain [`await` expressions].
103+
Async contexts are established by async blocks as well as the bodies of async functions, whose semantics are defined in terms of async blocks.
124104

125105
[`await` expressions]: await-expr.md
126106

127107
### Control-flow operators
128108

129-
Async blocks act like a function boundary, much like
130-
closures. Therefore, the `?` operator and `return` expressions both
131-
affect the output of the future, not the enclosing function or other
132-
context. That is, `return <expr>` from within a closure will return
133-
the result of `<expr>` as the output of the future. Similarly, if
134-
`<expr>?` propagates an error, that error is propagated as the result
135-
of the future.
109+
Async blocks act like a function boundary, much like closures.
110+
Therefore, the `?` operator and `return` expressions both affect the output of the future, not the enclosing function or other context.
111+
That is, `return <expr>` from within a closure will return the result of `<expr>` as the output of the future.
112+
Similarly, if `<expr>?` propagates an error, that error is propagated as the result of the future.
136113

137-
Finally, the `break` and `continue` keywords cannot be used to branch
138-
out from an async block. Therefore the following is illegal:
114+
Finally, the `break` and `continue` keywords cannot be used to branch out from an async block.
115+
Therefore the following is illegal:
139116

140117
```rust,edition2018,compile_fail
141118
loop {
@@ -153,8 +130,8 @@ loop {
153130
154131
_See [`unsafe` block](../unsafe-blocks.md) for more information on when to use `unsafe`_
155132

156-
A block of code can be prefixed with the `unsafe` keyword to permit [unsafe
157-
operations]. Examples:
133+
A block of code can be prefixed with the `unsafe` keyword to permit [unsafe operations].
134+
Examples:
158135

159136
```rust
160137
unsafe {
@@ -170,8 +147,7 @@ let a = unsafe { an_unsafe_fn() };
170147

171148
## Attributes on block expressions
172149

173-
[Inner attributes] are allowed directly after the opening brace of a block
174-
expression in the following situations:
150+
[Inner attributes] are allowed directly after the opening brace of a block expression in the following situations:
175151

176152
* [Function] and [method] bodies.
177153
* Loop bodies ([`loop`], [`while`], [`while let`], and [`for`]).
@@ -181,11 +157,9 @@ expression in the following situations:
181157
* A block expression as the tail expression of another block expression.
182158
<!-- Keep list in sync with expressions.md -->
183159

184-
The attributes that have meaning on a block expression are [`cfg`] and [the
185-
lint check attributes].
160+
The attributes that have meaning on a block expression are [`cfg`] and [the lint check attributes].
186161

187-
For example, this function returns `true` on unix platforms and `false` on other
188-
platforms.
162+
For example, this function returns `true` on unix platforms and `false` on other platforms.
189163

190164
```rust
191165
fn is_unix_platform() -> bool {

0 commit comments

Comments
 (0)