Skip to content

Commit 4676697

Browse files
committed
doc: mention moved and copied types. cc: #4217
1 parent bb66fce commit 4676697

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

doc/rust.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,14 @@ when evaluated in an _rvalue context_, it denotes the value held _in_ that memor
14651465
When an rvalue is used in lvalue context, a temporary un-named lvalue is created and used instead.
14661466
A temporary's lifetime equals the largest lifetime of any borrowed pointer that points to it.
14671467

1468+
#### Moved and copied types
1469+
1470+
When a [local variable](#memory-slots) is used as an [rvalue](#lvalues-rvalues-and-temporaries)
1471+
the variable will either be [moved](#move-expressions) or [copied](#copy-expressions),
1472+
depending on its type.
1473+
For types that contain mutable fields or [owning pointers](#owning-pointers), the variable is moved.
1474+
All other types are copied.
1475+
14681476

14691477
### Literal expressions
14701478

@@ -1787,7 +1795,7 @@ y.z <-> b.c;
17871795
An _assignment expression_ consists of an [lvalue](#lvalues-rvalues-and-temporaries) expression followed by an
17881796
equals sign (`=`) and an [rvalue](#lvalues-rvalues-and-temporaries) expression.
17891797

1790-
Evaluating an assignment expression copies the expression on the right-hand side and stores it in the location on the left-hand side.
1798+
Evaluating an assignment expression [either copies or moves](#moved-and-copied-types) its right-hand operand to its left-hand operand.
17911799

17921800
~~~~
17931801
# let mut x = 0;
@@ -1860,7 +1868,7 @@ copy.
18601868
as are raw and borrowed pointers.
18611869
[Owned boxes](#pointer-types), [owned vectors](#vector-types) and similar owned types are deep-copied.
18621870

1863-
Since the binary [assignment operator](#assignment-expressions) `=` performs a copy implicitly,
1871+
Since the binary [assignment operator](#assignment-expressions) `=` performs a copy or move implicitly,
18641872
the unary copy operator is typically only used to cause an argument to a function to be copied and passed by value.
18651873

18661874
An example of a copy expression:
@@ -1884,13 +1892,17 @@ move_expr : "move" expr ;
18841892
~~~~~~~~
18851893

18861894
A _unary move expression_ is similar to a [unary copy](#unary-copy-expressions) expression,
1887-
except that it can only be applied to an [lvalue](#lvalues-rvalues-and-temporaries),
1895+
except that it can only be applied to a [local variable](#memory-slots),
18881896
and it performs a _move_ on its operand, rather than a copy.
18891897
That is, the memory location denoted by its operand is de-initialized after evaluation,
18901898
and the resulting value is a shallow copy of the operand,
18911899
even if the operand is an [owning type](#type-kinds).
18921900

18931901

1902+
> **Note:** In future versions of Rust, `move` may be removed as a separate operator;
1903+
> moves are now [automatically performed](#moved-and-copied-types) for most cases `move` would be appropriate.
1904+
1905+
18941906
### Call expressions
18951907

18961908
~~~~~~~~ {.abnf .gram}

0 commit comments

Comments
 (0)