Skip to content

Commit a0f4b69

Browse files
committed
Referencify Assignment Operator
I'm being wish-washy with expression/operand in the syntax section. I'm not quite sure if we should call them `place operands` everywhere.
1 parent aa5e263 commit a0f4b69

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/expressions/operator-expr.md

+31-13
Original file line numberDiff line numberDiff line change
@@ -409,20 +409,35 @@ halfway between two floating point numbers.
409409
> _AssignmentExpression_ :\
410410
>    [_Expression_] `=` [_Expression_]
411411
412-
An _assignment expression_ consists of a [place expression] followed by an
413-
equals sign (`=`) and a [value expression]. Such an expression always has
414-
the [`unit` type].
415-
416-
Evaluating an assignment expression [drops](../destructors.md) the left-hand
417-
operand, unless it's an uninitialized local variable or field of a local variable,
418-
and [either copies or moves](../expressions.md#moved-and-copied-types) its
419-
right-hand operand to its left-hand operand. The left-hand operand must be a
420-
place expression: using a value expression results in a compiler error, rather
412+
An *assignment expression* moves a value into a specified place.
413+
414+
An assignment expression consists of a [mutable] [place expression], the
415+
*assigned place operand*, followed by an equals sign (`=`) and a [value
416+
expression], the *assigned value operand*.
417+
418+
Unlike other place operands, the assigned place operand must be a place
419+
expression. Attempting to use a value expression is a compiler error rather
421420
than promoting it to a temporary.
422421

422+
Evaluating assignment expressions begins by evaluating its operands. The
423+
assigned value operand is evaluated first, followed by the assigned place
424+
operand.
425+
426+
> **Note**: This is different than other expressions in that the right operand
427+
> is evaluated before the left one.
428+
429+
It then has the effect of first [dropping] the value at the assigned place,
430+
unless the place is an uninitialized local variable or field of a local
431+
variable. Next it either [copies or moves] the assigned value to the assigned
432+
place.
433+
434+
An assignment expression always produces [the unit value][unit].
435+
436+
Example:
437+
423438
```rust
424-
# let mut x = 0;
425-
# let y = 0;
439+
let mut x = 0;
440+
let y = 0;
426441
x = y;
427442
```
428443

@@ -444,7 +459,7 @@ x = y;
444459
The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, and `>>` operators may be
445460
composed with the `=` operator. The expression `place_exp OP= value` is
446461
equivalent to `place_expr = place_expr OP val`. For example, `x = x + 1` may be
447-
written as `x += 1`. Any such expression always has the [`unit` type].
462+
written as `x += 1`. Any such expression always has the [`unit` type][unit].
448463
These operators can all be overloaded using the trait with the same name as for
449464
the normal operation followed by 'Assign', for example, `std::ops::AddAssign`
450465
is used to overload `+=`. As with `=`, `place_expr` must be a [place
@@ -456,11 +471,14 @@ x += 4;
456471
assert_eq!(x, 14);
457472
```
458473

474+
[copies or moves]: ../expressions.md#moved-and-copied-types
475+
[dropping]: ../destructors.md
476+
[mutable]: ../expressions.md#mutability
459477
[place expression]: ../expressions.md#place-expressions-and-value-expressions
478+
[unit]: ../types/tuple.md
460479
[value expression]: ../expressions.md#place-expressions-and-value-expressions
461480
[temporary value]: ../expressions.md#temporaries
462481
[float-float]: https://github.com/rust-lang/rust/issues/15536
463-
[`unit` type]: ../types/tuple.md
464482
[Function pointer]: ../types/function-pointer.md
465483
[Function item]: ../types/function-item.md
466484

0 commit comments

Comments
 (0)