Skip to content

Commit

Permalink
Referencify Assignment Operator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Havvy committed Nov 20, 2020
1 parent aa5e263 commit a0f4b69
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,20 +409,35 @@ halfway between two floating point numbers.
> _AssignmentExpression_ :\
>    [_Expression_] `=` [_Expression_]
An _assignment expression_ consists of a [place expression] followed by an
equals sign (`=`) and a [value expression]. Such an expression always has
the [`unit` type].

Evaluating an assignment expression [drops](../destructors.md) the left-hand
operand, unless it's an uninitialized local variable or field of a local variable,
and [either copies or moves](../expressions.md#moved-and-copied-types) its
right-hand operand to its left-hand operand. The left-hand operand must be a
place expression: using a value expression results in a compiler error, rather
An *assignment expression* moves a value into a specified place.

An assignment expression consists of a [mutable] [place expression], the
*assigned place operand*, followed by an equals sign (`=`) and a [value
expression], the *assigned value operand*.

Unlike other place operands, the assigned place operand must be a place
expression. Attempting to use a value expression is a compiler error rather
than promoting it to a temporary.

Evaluating assignment expressions begins by evaluating its operands. The
assigned value operand is evaluated first, followed by the assigned place
operand.

> **Note**: This is different than other expressions in that the right operand
> is evaluated before the left one.
It then has the effect of first [dropping] the value at the assigned place,
unless the place is an uninitialized local variable or field of a local
variable. Next it either [copies or moves] the assigned value to the assigned
place.

An assignment expression always produces [the unit value][unit].

Example:

```rust
# let mut x = 0;
# let y = 0;
let mut x = 0;
let y = 0;
x = y;
```

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

[copies or moves]: ../expressions.md#moved-and-copied-types
[dropping]: ../destructors.md
[mutable]: ../expressions.md#mutability
[place expression]: ../expressions.md#place-expressions-and-value-expressions
[unit]: ../types/tuple.md
[value expression]: ../expressions.md#place-expressions-and-value-expressions
[temporary value]: ../expressions.md#temporaries
[float-float]: https://github.com/rust-lang/rust/issues/15536
[`unit` type]: ../types/tuple.md
[Function pointer]: ../types/function-pointer.md
[Function item]: ../types/function-item.md

Expand Down

0 comments on commit a0f4b69

Please sign in to comment.