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
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 expressions of uniform type enclosed in square brackets.
14
18
This produces an array containing each of these values in the order they are written.
15
19
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.
20
+
The syntax for the second form is two expressions separated by a semicolon (`;`) enclosed in square brackets.
21
+
The expression before the `;` is called the *repeat operand*.
22
+
The expression after the `;` is called the *length operand*.
23
+
It must have type `usize` and be a [constant expression], such as a [literal] or a [constant item].
24
+
An array expression of this form creates an array with the length of the value of the legnth operand with each element a copy of the repeat operand.
25
+
That is, `[a; b]` creates an array containing `b` copies of the value of `a`.
26
+
If the length operand has a value greater than 1 then this requires that the type of the repeat operand is [`Copy`] or that it must be a [path] to a constant item.
20
27
21
-
When the repeat expression `a`is a constant item, it is evaluated `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.
28
+
When the repeat operand is a constant item, it is evaluated the length operand's value times.
29
+
If that value is `0`, then the constant item is not evaluated at all.
30
+
For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied the length operand's value times.
24
31
25
32
<divclass="warning">
26
33
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.
34
+
Warning: In the case where the length operand is 0, and the repeat operand 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
35
See [issue #74836](https://github.com/rust-lang/rust/issues/74836).
[Array] and [slice]-typed expressions can be indexed by writing a square-bracket-enclosed expression of type `usize` (the index) after them.
59
+
[Array] and [slice]-typed values can be indexed by writing a square-bracket-enclosed expression of type `usize` (the index) after them.
53
60
When the array is mutable, the resulting [memory location] can be assigned to.
54
61
55
62
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.
@@ -91,4 +98,5 @@ The array index expression can be implemented for types other than arrays and sl
Copy file name to clipboardExpand all lines: src/expressions/await-expr.md
+17-19
Original file line number
Diff line number
Diff line change
@@ -4,45 +4,32 @@
4
4
> _AwaitExpression_ :\
5
5
> [_Expression_]`.``await`
6
6
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 expression with a type that implements the [Future] trait, called the *future operand*, then the token `.`, and then the `await` keyword.
7
9
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.
10
10
11
-
More specifically, an `<expr>.await` expression has the following effect.
11
+
More specifically, an await expression has the following effect.
12
12
13
-
1. Evaluate `<expr>` to a [future]`tmp`;
13
+
1. Evaluate the future operand to a [future]`tmp`;
14
14
2. Pin `tmp` using [`Pin::new_unchecked`];
15
15
3. This pinned future is then polled by calling the [`Future::poll`] method and passing it the current [task context](#task-context);
16
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
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.
Copy file name to clipboardExpand all lines: src/expressions/block-expr.md
+38-37
Original file line number
Diff line number
Diff line change
@@ -16,16 +16,19 @@ A *block expression*, or *block*, is a control flow expression and anonymous nam
16
16
As a control flow expression, a block sequentially executes its component non-item declaration statements and then its final optional expression.
17
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
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.
19
+
The syntax for a block is `{`, then any [inner attributes], then any number of [statements], then an optional expression, called the final 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.
25
+
23
26
Furthermore, extra semicolons between statements are allowed, but these semicolons do not affect semantics.
24
27
25
28
When evaluating a block expression, each statement, except for item declaration statements, is executed sequentially.
26
-
Then the final expression is executed, if given.
29
+
Then the final operand is executed, if given.
27
30
28
-
The type of a block is the type of the final expression, or `()` if the final expression is omitted.
31
+
The type of a block is the type of the final operand, or `()` if the final operand is omitted.
29
32
30
33
```rust
31
34
# fnfn_call() {}
@@ -43,36 +46,37 @@ assert_eq!(5, five);
43
46
44
47
> 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.
45
48
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.
49
-
50
-
```rust,compile_fail
51
-
struct Struct;
52
-
53
-
impl Struct {
54
-
fn consume_self(self) {}
55
-
fn borrow_self(&self) {}
56
-
}
57
-
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();
49
+
Blocks are always [value expressions] and evaluate the last operand in value expression context.
63
50
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.
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 expression, called the *function operand*, followed by a parenthesized comma-separated list of expression, called the *argument operands*.
12
12
If the function eventually returns, then the expression completes.
13
-
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.
13
+
For [non-function types], 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.
14
14
An automatic borrow will be taken if needed.
15
-
Rust will also automatically dereference `f` as required.
15
+
The function operand will also be [automatically dereferenced] as required.
Rust treats all function calls as sugar for a more explicit, [fully-qualified syntax].
27
-
Upon compilation, Rust will desugar all function calls into the explicit form.
28
-
Rust may sometimes require you to qualify function calls with trait, depending on the ambiguity of a call in light of in-scope items.
27
+
All function calls are sugar for a more explicit [fully-qualified syntax].
28
+
Function calls may need to be fully qualified, depending on the ambiguity of a call in light of in-scope items.
29
29
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.
30
+
> **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.
31
+
> However, these terms lack descriptive power and potentially confuse the issue at hand.
32
+
> We mention them here for searchability's sake.
33
33
34
34
Several situations often occur which result in ambiguities about the receiver or referent of method or associated function calls.
35
35
These situations may include:
@@ -92,4 +92,6 @@ Refer to [RFC 132] for further details and motivations.
0 commit comments