|
45 | 45 | An expression may have two roles: it always produces a *value*, and it may have
|
46 | 46 | *effects* (otherwise known as "side effects"). An expression *evaluates to* a
|
47 | 47 | value, and has effects during *evaluation*. Many expressions contain
|
48 |
| -sub-expressions (operands). The meaning of each kind of expression dictates |
49 |
| -several things: |
| 48 | +sub-expressions, called the *operands* of the expression. The meaning of each |
| 49 | +kind of expression dictates several things: |
50 | 50 |
|
51 |
| -* Whether or not to evaluate the sub-expressions when evaluating the expression |
52 |
| -* The order in which to evaluate the sub-expressions |
53 |
| -* How to combine the sub-expressions' values to obtain the value of the |
54 |
| - expression |
| 51 | +* Whether or not to evaluate the operands when evaluating the expression |
| 52 | +* The order in which to evaluate the operands |
| 53 | +* How to combine the operands' values to obtain the value of the expression |
55 | 54 |
|
56 | 55 | In this way, the structure of expressions dictates the structure of execution.
|
57 | 56 | Blocks are just another kind of expression, so blocks, statements, expressions,
|
@@ -85,6 +84,58 @@ in the order given by their associativity.
|
85 | 84 | | `=` `+=` `-=` `*=` `/=` `%=` <br> `&=` <code>|=</code> `^=` `<<=` `>>=` | right to left |
|
86 | 85 | | `return` `break` closures | |
|
87 | 86 |
|
| 87 | +## Evaluation order of operands |
| 88 | + |
| 89 | +The following list of expressions all evaluate their operands the same way, as |
| 90 | +described after the list. Other expressions either don't take operands or |
| 91 | +evaluate them conditionally as described on their respective pages. |
| 92 | + |
| 93 | +* Dereference expression |
| 94 | +* Error propagation expression |
| 95 | +* Negation expression |
| 96 | +* Arithmetic and logical binary operators |
| 97 | +* Comparison operators |
| 98 | +* Type cast expression |
| 99 | +* Grouped expression |
| 100 | +* Array expression |
| 101 | +* Await expression |
| 102 | +* Index expression |
| 103 | +* Tuple expression |
| 104 | +* Tuple index expression |
| 105 | +* Struct expression |
| 106 | +* Enumeration variant expression |
| 107 | +* Call expression |
| 108 | +* Method call expression |
| 109 | +* Field expression |
| 110 | +* Break expression |
| 111 | +* Range expression |
| 112 | +* Return expression |
| 113 | + |
| 114 | +The operands of these expressions are evaluated prior to applying the effects of |
| 115 | +the expression. Expressions taking multiple operands are evaluated left to right |
| 116 | +as written in the source code. |
| 117 | + |
| 118 | +> **Note**: Which subexpressions are the operands of an expression is |
| 119 | +> determined by expression precedence as per the previous section. |
| 120 | +
|
| 121 | +For example, the two `next` method calls will always be called in the same |
| 122 | +order: |
| 123 | + |
| 124 | +```rust |
| 125 | +# // Using vec instead of array to avoid references |
| 126 | +# // since there is no stable owned array iterator |
| 127 | +# // at the time this example was written. |
| 128 | +let mut one_two = vec![1, 2].into_iter(); |
| 129 | +assert_eq!( |
| 130 | + (1, 2), |
| 131 | + (one_two.next().unwrap(), one_two.next().unwrap()) |
| 132 | +); |
| 133 | +``` |
| 134 | + |
| 135 | +> **Note**: Since this is applied recursively, these expressions are also |
| 136 | +> evaluated from innermost to outermost, ignoring siblings until there are no |
| 137 | +> inner subexpressions. |
| 138 | +
|
88 | 139 | ## Place Expressions and Value Expressions
|
89 | 140 |
|
90 | 141 | Expressions are divided into two main categories: place expressions and
|
|
0 commit comments