Skip to content

Commit 2af6d3c

Browse files
authored
Merge pull request #1277 from Havvy/one-line-expressions
One line one sentence for expressions and statements main chapters
2 parents 3d1ca95 + 45c47cd commit 2af6d3c

File tree

3 files changed

+79
-132
lines changed

3 files changed

+79
-132
lines changed

src/expressions.md

+52-85
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,24 @@
4242
>       | [_MatchExpression_]\
4343
>    )
4444
45-
An expression may have two roles: it always produces a *value*, and it may have
46-
*effects* (otherwise known as "side effects"). An expression *evaluates to* a
47-
value, and has effects during *evaluation*. Many expressions contain
48-
sub-expressions, called the *operands* of the expression. The meaning of each
49-
kind of expression dictates several things:
45+
An expression may have two roles: it always produces a *value*, and it may have *effects* (otherwise known as "side effects").
46+
An expression *evaluates to* a value, and has effects during *evaluation*.
47+
Many expressions contain sub-expressions, called the *operands* of the expression.
48+
The meaning of each kind of expression dictates several things:
5049

5150
* Whether or not to evaluate the operands when evaluating the expression
5251
* The order in which to evaluate the operands
5352
* How to combine the operands' values to obtain the value of the expression
5453

5554
In this way, the structure of expressions dictates the structure of execution.
56-
Blocks are just another kind of expression, so blocks, statements, expressions,
57-
and blocks again can recursively nest inside each other to an arbitrary depth.
55+
Blocks are just another kind of expression, so blocks, statements, expressions, and blocks again can recursively nest inside each other to an arbitrary depth.
5856

59-
> **Note**: We give names to the operands of expressions so that we may discuss
60-
> them, but these names are not stable and may be changed.
57+
> **Note**: We give names to the operands of expressions so that we may discuss them, but these names are not stable and may be changed.
6158
6259
## Expression precedence
6360

64-
The precedence of Rust operators and expressions is ordered as follows, going
65-
from strong to weak. Binary Operators at the same precedence level are grouped
66-
in the order given by their associativity.
61+
The precedence of Rust operators and expressions is ordered as follows, going from strong to weak.
62+
Binary Operators at the same precedence level are grouped in the order given by their associativity.
6763

6864
| Operator/Expression | Associativity |
6965
|-----------------------------|---------------------|
@@ -89,9 +85,8 @@ in the order given by their associativity.
8985

9086
## Evaluation order of operands
9187

92-
The following list of expressions all evaluate their operands the same way, as
93-
described after the list. Other expressions either don't take operands or
94-
evaluate them conditionally as described on their respective pages.
88+
The following list of expressions all evaluate their operands the same way, as described after the list.
89+
Other expressions either don't take operands or evaluate them conditionally as described on their respective pages.
9590

9691
* Dereference expression
9792
* Error propagation expression
@@ -113,15 +108,13 @@ evaluate them conditionally as described on their respective pages.
113108
* Range expression
114109
* Return expression
115110

116-
The operands of these expressions are evaluated prior to applying the effects of
117-
the expression. Expressions taking multiple operands are evaluated left to right
118-
as written in the source code.
111+
The operands of these expressions are evaluated prior to applying the effects of the expression.
112+
Expressions taking multiple operands are evaluated left to right as written in the source code.
119113

120114
> **Note**: Which subexpressions are the operands of an expression is
121115
> determined by expression precedence as per the previous section.
122116
123-
For example, the two `next` method calls will always be called in the same
124-
order:
117+
For example, the two `next` method calls will always be called in the same order:
125118

126119
```rust
127120
# // Using vec instead of array to avoid references
@@ -134,23 +127,18 @@ assert_eq!(
134127
);
135128
```
136129

137-
> **Note**: Since this is applied recursively, these expressions are also
138-
> evaluated from innermost to outermost, ignoring siblings until there are no
139-
> inner subexpressions.
130+
> **Note**: Since this is applied recursively, these expressions are also evaluated from innermost to outermost, ignoring siblings until there are no inner subexpressions.
140131
141132
## Place Expressions and Value Expressions
142133

143-
Expressions are divided into two main categories: place expressions and value
144-
expressions; there is also a third, minor category of expressions called
145-
assignee expressions. Within each expression, operands may likewise occur in
146-
either place context or value context. The evaluation of an expression depends
147-
both on its own category and the context it occurs within.
134+
Expressions are divided into two main categories: place expressions and value expressions;
135+
there is also a third, minor category of expressions called assignee expressions.
136+
Within each expression, operands may likewise occur in either place context or value context.
137+
The evaluation of an expression depends both on its own category and the context it occurs within.
148138

149-
A *place expression* is an expression that represents a memory location. These
150-
expressions are [paths] which refer to local variables, [static variables],
151-
[dereferences][deref] (`*expr`), [array indexing] expressions (`expr[expr]`),
152-
[field] references (`expr.f`) and parenthesized place expressions. All other
153-
expressions are value expressions.
139+
A *place expression* is an expression that represents a memory location.
140+
These expressions are [paths] which refer to local variables, [static variables], [dereferences][deref] (`*expr`), [array indexing] expressions (`expr[expr]`), [field] references (`expr.f`) and parenthesized place expressions.
141+
All other expressions are value expressions.
154142

155143
A *value expression* is an expression that represents an actual value.
156144

@@ -166,11 +154,10 @@ The following contexts are *place expression* contexts:
166154
expression.
167155
* The base of a [functional update] struct expression.
168156

169-
> Note: Historically, place expressions were called *lvalues* and value
170-
> expressions were called *rvalues*.
157+
> Note: Historically, place expressions were called *lvalues* and value expressions were called *rvalues*.
171158
172-
An *assignee expression* is an expression that appears in the left operand of an
173-
[assignment][assign] expression. Explicitly, the assignee expressions are:
159+
An *assignee expression* is an expression that appears in the left operand of an [assignment][assign] expression.
160+
Explicitly, the assignee expressions are:
174161

175162
- Place expressions.
176163
- [Underscores][_UnderscoreExpression_].
@@ -185,59 +172,49 @@ Arbitrary parenthesisation is permitted inside assignee expressions.
185172

186173
### Moved and copied types
187174

188-
When a place expression is evaluated in a value expression context, or is bound
189-
by value in a pattern, it denotes the value held _in_ that memory location. If
190-
the type of that value implements [`Copy`], then the value will be copied. In
191-
the remaining situations, if that type is [`Sized`], then it may be possible to
192-
move the value. Only the following place expressions may be moved out of:
175+
When a place expression is evaluated in a value expression context, or is bound by value in a pattern, it denotes the value held _in_ that memory location.
176+
If the type of that value implements [`Copy`], then the value will be copied.
177+
In the remaining situations, if that type is [`Sized`], then it may be possible to move the value.
178+
Only the following place expressions may be moved out of:
193179

194180
* [Variables] which are not currently borrowed.
195181
* [Temporary values](#temporaries).
196-
* [Fields][field] of a place expression which can be moved out of and
197-
don't implement [`Drop`].
198-
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and
199-
that can also be moved out of.
182+
* [Fields][field] of a place expression which can be moved out of and don't implement [`Drop`].
183+
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and that can also be moved out of.
200184

201-
After moving out of a place expression that evaluates to a local variable, the
202-
location is deinitialized and cannot be read from again until it is
203-
reinitialized. In all other cases, trying to use a place expression in a value
204-
expression context is an error.
185+
After moving out of a place expression that evaluates to a local variable, the location is deinitialized and cannot be read from again until it is reinitialized.
186+
In all other cases, trying to use a place expression in a value expression context is an error.
205187

206188
### Mutability
207189

208-
For a place expression to be [assigned][assign] to, mutably [borrowed][borrow],
209-
[implicitly mutably borrowed], or bound to a pattern containing `ref mut`, it
210-
must be _mutable_. We call these *mutable place expressions*. In contrast,
211-
other place expressions are called *immutable place expressions*.
190+
For a place expression to be [assigned][assign] to, mutably [borrowed][borrow], [implicitly mutably borrowed], or bound to a pattern containing `ref mut`, it must be _mutable_.
191+
We call these *mutable place expressions*.
192+
In contrast, other place expressions are called *immutable place expressions*.
212193

213194
The following expressions can be mutable place expression contexts:
214195

215196
* Mutable [variables] which are not currently borrowed.
216197
* [Mutable `static` items].
217198
* [Temporary values].
218-
* [Fields][field]: this evaluates the subexpression in a mutable place
219-
expression context.
199+
* [Fields][field]: this evaluates the subexpression in a mutable place expression context.
220200
* [Dereferences][deref] of a `*mut T` pointer.
221-
* Dereference of a variable, or field of a variable, with type `&mut T`. Note:
222-
This is an exception to the requirement of the next rule.
223-
* Dereferences of a type that implements `DerefMut`: this then requires that
224-
the value being dereferenced is evaluated in a mutable place expression context.
225-
* [Array indexing] of a type that implements `IndexMut`: this
226-
then evaluates the value being indexed, but not the index, in mutable place
227-
expression context.
201+
* Dereference of a variable, or field of a variable, with type `&mut T`.
202+
Note: This is an exception to the requirement of the next rule.
203+
* Dereferences of a type that implements `DerefMut`:
204+
this then requires that the value being dereferenced is evaluated in a mutable place expression context.
205+
* [Array indexing] of a type that implements `IndexMut`:
206+
this then evaluates the value being indexed, but not the index, in mutable place expression context.
228207

229208
### Temporaries
230209

231-
When using a value expression in most place expression contexts, a temporary
232-
unnamed memory location is created and initialized to that value. The expression
233-
evaluates to that location instead, except if [promoted] to a `static`. The
234-
[drop scope] of the temporary is usually the end of the enclosing statement.
210+
When using a value expression in most place expression contexts, a temporary unnamed memory location is created and initialized to that value.
211+
The expression evaluates to that location instead, except if [promoted] to a `static`.
212+
The [drop scope] of the temporary is usually the end of the enclosing statement.
235213

236214
### Implicit Borrows
237215

238-
Certain expressions will treat an expression as a place expression by implicitly
239-
borrowing it. For example, it is possible to compare two unsized [slices][slice] for
240-
equality directly, because the `==` operator implicitly borrows its operands:
216+
Certain expressions will treat an expression as a place expression by implicitly borrowing it.
217+
For example, it is possible to compare two unsized [slices][slice] for equality directly, because the `==` operator implicitly borrows its operands:
241218

242219
```rust
243220
# let c = [1, 2, 3];
@@ -264,31 +241,21 @@ Implicit borrows may be taken in the following expressions:
264241

265242
## Overloading Traits
266243

267-
Many of the following operators and expressions can also be overloaded for
268-
other types using traits in `std::ops` or `std::cmp`. These traits also
269-
exist in `core::ops` and `core::cmp` with the same names.
244+
Many of the following operators and expressions can also be overloaded for other types using traits in `std::ops` or `std::cmp`.
245+
These traits also exist in `core::ops` and `core::cmp` with the same names.
270246

271247
## Expression Attributes
272248

273-
[Outer attributes][_OuterAttribute_] before an expression are allowed only in
274-
a few specific cases:
249+
[Outer attributes][_OuterAttribute_] before an expression are allowed only in a few specific cases:
275250

276251
* Before an expression used as a [statement].
277-
* Elements of [array expressions], [tuple expressions], [call expressions],
278-
and tuple-style [struct] expressions.
279-
<!--
280-
These were likely stabilized inadvertently.
281-
See https://github.com/rust-lang/rust/issues/32796 and
282-
https://github.com/rust-lang/rust/issues/15701
283-
-->
252+
* Elements of [array expressions], [tuple expressions], [call expressions], and tuple-style [struct] expressions.
284253
* The tail expression of [block expressions].
285254
<!-- Keep list in sync with block-expr.md -->
286255

287256
They are never allowed before:
288257
* [Range][_RangeExpression_] expressions.
289-
* Binary operator expressions ([_ArithmeticOrLogicalExpression_],
290-
[_ComparisonExpression_], [_LazyBooleanExpression_], [_TypeCastExpression_],
291-
[_AssignmentExpression_], [_CompoundAssignmentExpression_]).
258+
* Binary operator expressions ([_ArithmeticOrLogicalExpression_], [_ComparisonExpression_], [_LazyBooleanExpression_], [_TypeCastExpression_], [_AssignmentExpression_], [_CompoundAssignmentExpression_]).
292259

293260

294261
[block expressions]: expressions/block-expr.md

src/statements-and-expressions.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# Statements and expressions
22

3-
Rust is _primarily_ an expression language. This means that most forms of
4-
value-producing or effect-causing evaluation are directed by the uniform syntax
5-
category of _expressions_. Each kind of expression can typically _nest_ within
6-
each other kind of expression, and rules for evaluation of expressions involve
7-
specifying both the value produced by the expression and the order in which its
8-
sub-expressions are themselves evaluated.
3+
Rust is _primarily_ an expression language.
4+
This means that most forms of value-producing or effect-causing evaluation are directed by the uniform syntax category of _expressions_.
5+
Each kind of expression can typically _nest_ within each other kind of expression, and rules for evaluation of expressions involve specifying both the value produced by the expression and the order in which its sub-expressions are themselves evaluated.
96

10-
In contrast, statements serve _mostly_ to contain and explicitly
11-
sequence expression evaluation.
7+
In contrast, statements serve _mostly_ to contain and explicitly sequence expression evaluation.

0 commit comments

Comments
 (0)