Skip to content

Commit 16c2cc2

Browse files
authored
Merge pull request #126 from matthewjasper/expressions-fixes
Small fixes to the expressions section
2 parents 36adc6a + aec44d6 commit 16c2cc2

File tree

5 files changed

+62
-59
lines changed

5 files changed

+62
-59
lines changed

src/expressions.md

+48-24
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@ In this way, the structure of expressions dictates the structure of execution.
1515
Blocks are just another kind of expression, so blocks, statements, expressions,
1616
and blocks again can recursively nest inside each other to an arbitrary depth.
1717

18+
## Expression precedence
19+
20+
The precedence of Rust operators and expressions is ordered as follows, going
21+
from strong to weak. Binary Operators at the same precedence level are
22+
evaluated in the order given by their associativity.
23+
24+
| Operator/Expression | Associativity |
25+
|-----------------------------|---------------------|
26+
| Paths | |
27+
| Method calls | |
28+
| Field expressions | left to right |
29+
| Function calls, array indexing | |
30+
| `?` | |
31+
| Unary `-` `*` `!` `&` `&mut` | |
32+
| `as` `:` | left to right |
33+
| `*` `/` `%` | left to right |
34+
| `+` `-` | left to right |
35+
| `<<` `>>` | left to right |
36+
| `&` | left to right |
37+
| `^` | left to right |
38+
| <code>&#124;</code> | left to right |
39+
| `==` `!=` `<` `>` `<=` `>=` | Require parentheses |
40+
| `&&` | left to right |
41+
| <code>&#124;&#124;</code> | left to right |
42+
| `..` `...` | Require parentheses |
43+
| `<-` | right to left |
44+
| `=` `+=` `-=` `*=` `/=` `%=` <br> `&=` <code>&#124;=</code> `^=` `<<=` `>>=` | right to left |
45+
| `return` `break` closures | |
46+
1847
## Lvalues and rvalues
1948

2049
Expressions are divided into two main categories: _lvalues_ and _rvalues_.
@@ -23,25 +52,24 @@ or _rvalue context_. The evaluation of an expression depends both on its own
2352
category and the context it occurs within.
2453

2554
An lvalue is an expression that represents a memory location. These expressions
26-
are paths which refer to local variables, function and method arguments, or
27-
static variables, [dereferences]&nbsp;(`*expr`), [array indexing] expressions
28-
(`expr[expr]`), [field] references (`expr.f`) and parenthesized lvalue
29-
expressions. All other expressions are rvalues.
55+
are [paths](#path-expressions) which refer to local variables, static
56+
variables, function parameters, [dereferences]&nbsp;(`*expr`), [array indexing]
57+
expressions (`expr[expr]`), [field] references (`expr.f`) and parenthesized
58+
lvalue expressions. All other expressions are rvalues.
3059

3160
The left operand of an [assign]ment or [compound assignment] expression is an
3261
lvalue context, as is the single operand of a unary [borrow], and the operand
3362
of any [implicit borrow](#implicit-borrows). The discriminant or subject of a
34-
[match] expression and right side of a `let` binding may be an lvalue context,
35-
if ref bindings are made, but is otherwise an rvalue context. All other
36-
expression contexts are rvalue contexts.
63+
[match] expression and right side of a `let` is also an lvalue context. All
64+
other expression contexts are rvalue contexts.
3765

3866
### Moved and copied types
3967

40-
When an lvalue is evaluated in an _rvalue context_, it denotes the value held
41-
_in_ that memory location. If value is of a type that implements `Copy`, then
42-
the value will be copied. In the remaining situations if the type of the value
43-
is [`Sized`](the-sized-trait.html) it may be possible to move the value. Only
44-
the following lvalues may be moved out of:
68+
When an lvalue is evaluated in an _rvalue context_ or is bound by value in a
69+
pattern, it denotes the value held _in_ that memory location. If value is of a
70+
type that implements `Copy`, then the value will be copied. In the remaining
71+
situations if the type of the value is [`Sized`](the-sized-trait.html) it may
72+
be possible to move the value. Only the following lvalues may be moved out of:
4573

4674
* [Variables](variables.html) which are not currently borrowed.
4775
* [Temporary values](#temporary-lifetimes).
@@ -88,11 +116,11 @@ That is, the promoted expression can be evaluated at compile-time and the
88116
resulting value does not contain interior mutability or destructors (these
89117
properties are determined based on the value where possible, e.g. `&None`
90118
always has the type `&'static Option<_>`, as it contains nothing disallowed).
91-
Otherwise, the lifetime of temporary values is typically
119+
Otherwise, the lifetime of temporary values is typically
92120

93-
- the innermost enclosing statement; the tail expression of a block is
121+
- the innermost enclosing statement; the tail expression of a block is
94122
considered part of the statement that encloses the block, or
95-
- the condition expression or the loop conditional expression if the
123+
- the condition expression or the loop conditional expression if the
96124
temporary is created in the condition expression of an `if` or an `if`/`else`
97125
or in the loop conditional expression of a `while` expression.
98126

@@ -119,7 +147,7 @@ Here are some examples:
119147
an rvalue. As the temporary is created in the condition expression
120148
of an `if`/`else`, it will be freed at the end of the condition expression
121149
(in this example before the call to `bar` or `baz` is made).
122-
- `let x = if temp().must_run_bar {bar()} else {baz()};`.
150+
- `let x = if temp().must_run_bar {bar()} else {baz()};`.
123151
Here we assume the type of `temp()` is a struct with a boolean field
124152
`must_run_bar`. As the previous example, the temporary corresponding to
125153
`temp()` will be freed at the end of the condition expression.
@@ -186,29 +214,25 @@ also constant expressions:
186214
* [Literals].
187215
* [Paths] to [functions](items/functions.html) and constants.
188216
Recursively defining constants is not allowed.
189-
* Paths to statics, so long as only their address, not their value, is used.
190-
This includes using their value indirectly through a complicated expression.
191-
\*
192217
* [Tuple expressions].
193218
* [Array expressions].
194219
* [Struct] expressions, where the type does not implement [`Drop`](the-drop-trait.html).
195220
* [Enum variant] expressions, where the enumeration type does not implement `Drop`.
196-
* [Block expressions]&nbsp;(and `unsafe` blocks) which contain only items and
221+
* [Block expressions]&nbsp;(and `unsafe` blocks) which only contain items and
197222
possibly a (constant) tail expression.
198223
* [Field] expressions.
199224
* Index expressions, [array indexing] or [slice] with a `usize`.
200225
* [Range expressions].
201226
* [Closure expressions] which don't capture variables from the environment.
202227
* Built in [negation], [arithmetic, logical], [comparison] or [lazy boolean]
203228
operators used on integer and floating point types, `bool` and `char`.
204-
* Shared [borrow].
205-
* The [dereference operator], but not to circumvent the rule on statics.
229+
* Shared [borrow], except if applied to a type with [interior
230+
mutability](interior-mutability.html).
231+
* The [dereference operator].
206232
* [Grouped] expressions.
207233
* [Cast] expressions, except pointer to address and
208234
function pointer to address casts.
209235

210-
\* Only in static items.
211-
212236
## Overloading Traits
213237

214238
Many of the following operators and expressions can also be overloaded for

src/expressions/field-expr.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ Also, if the type of the expression to the left of the dot is a pointer, it is
2323
automatically dereferenced as many times as necessary to make the field access
2424
possible. In cases of ambiguity, we prefer fewer autoderefs to more.
2525

26-
Finally the fields of a struct, a reference to a struct are treated as separate
27-
entities when borrowing. If the struct does not implement
28-
[`Drop`](the-drop-trait.html) this also applies to moving out of each of its fields
29-
where possible. This also does not apply if automatic dereferencing is done
30-
though user defined types.
26+
Finally, the fields of a struct or a reference to a struct are treated as
27+
separate entities when borrowing. If the struct does not implement
28+
[`Drop`](the-drop-trait.html) and is stored in a local variable, this also
29+
applies to moving out of each of its fields. This also does not apply if
30+
automatic dereferencing is done though user defined types.
3131

3232
```rust
33-
# struct A { f1: String, f2: String, f3: String }
34-
# let mut x = A {
33+
struct A { f1: String, f2: String, f3: String }
34+
let mut x: A;
35+
# x = A {
3536
# f1: "f1".to_string(),
3637
# f2: "f2".to_string(),
3738
# f3: "f3".to_string()

src/expressions/method-call-expr.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ let log_pi = pi.unwrap_or(1.0).log(2.72);
1515
```
1616

1717
When resolving method calls on an expression of type `A`, Rust will use the
18-
following order:
18+
following order, only looking at methods that are
19+
[visible](#visibility-and-privacy.html) and traits that are in scope:
1920

2021
1. Inherent methods, with receiver of type `A`, `&A`, `&mut A`.
2122
1. Trait methods with receiver of type `A`.

src/expressions/operator-expr.md

-26
Original file line numberDiff line numberDiff line change
@@ -331,29 +331,3 @@ let mut x = 10;
331331
x += 4;
332332
assert_eq!(x, 14);
333333
```
334-
335-
## Operator precedence
336-
337-
The precedence of Rust operators is ordered as follows, going from strong to
338-
weak. Binary Operators at the same precedence level are evaluated in the order
339-
given by their associativity.
340-
341-
342-
| Operator | Associativity |
343-
|-----------------------------|---------------------|
344-
| `?` | |
345-
| Unary `-` `*` `!` `&` `&mut` | |
346-
| `as` `:` | left to right |
347-
| `*` `/` `%` | left to right |
348-
| `+` `-` | left to right |
349-
| `<<` `>>` | left to right |
350-
| `&` | left to right |
351-
| `^` | left to right |
352-
| <code>&#124;</code> | left to right |
353-
| `==` `!=` `<` `>` `<=` `>=` | Require parentheses |
354-
| `&&` | left to right |
355-
| <code>&#124;&#124;</code> | left to right |
356-
| `..` `...` | Require parentheses |
357-
| `<-` | right to left |
358-
| `=` `+=` `-=` `*=` `/=` `%=` <br> `&=` <code>&#124;=</code> `^=` `<<=` `>>=` | right to left |
359-

src/items/static-items.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ statics:
1414
* Statics may not contain any destructors.
1515
* The types of static values must ascribe to `Sync` to allow thread-safe
1616
access.
17-
* Statics may not refer to other statics by value, only by reference.
17+
* Statics allow using paths to statics in the
18+
[constant-expression](#expresions.html#constant-expressions) used to
19+
initialize them, but statics may not refer to other statics by value, only by
20+
reference.
1821
* Constants cannot refer to statics.
1922

2023
Constants should in general be preferred over statics, unless large amounts of

0 commit comments

Comments
 (0)