mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-16 13:48:52 +02:00
Remove statement that data is moved when assigning (#982)
* Remove statement that data is moved when assigning The distinction between non-`Copy` and `Copy` types is tricky to explain. One problem is that people often focus on _moving_ vs _copying_ when both variable types always copy data. This PR removes the statement about moving data (since that is wrong on its own). * Apply suggestions from code review Co-authored-by: Dustin J. Mitchell <djmitche@google.com> * Apply suggestions from code review Co-authored-by: Dustin J. Mitchell <djmitche@google.com> --------- Co-authored-by: Dustin J. Mitchell <djmitche@google.com>
This commit is contained in:
parent
486458c72d
commit
d4c08e6c9d
@ -1,6 +1,6 @@
|
||||
# Move Semantics
|
||||
|
||||
An assignment will transfer ownership between variables:
|
||||
An assignment will transfer _ownership_ between variables:
|
||||
|
||||
```rust,editable
|
||||
fn main() {
|
||||
@ -12,8 +12,7 @@ fn main() {
|
||||
```
|
||||
|
||||
* The assignment of `s1` to `s2` transfers ownership.
|
||||
* The data was _moved_ from `s1` and `s1` is no longer accessible.
|
||||
* When `s1` goes out of scope, nothing happens: it has no ownership.
|
||||
* When `s1` goes out of scope, nothing happens: it does not own anything.
|
||||
* When `s2` goes out of scope, the string data is freed.
|
||||
* There is always _exactly_ one variable binding which owns a value.
|
||||
|
||||
@ -21,6 +20,10 @@ fn main() {
|
||||
|
||||
* Mention that this is the opposite of the defaults in C++, which copies by value unless you use `std::move` (and the move constructor is defined!).
|
||||
|
||||
* It is only the ownership that moves. Whether any machine code is generated to manipulate the data itself is a matter of optimization, and such copies are aggressively optimized away.
|
||||
|
||||
* Simple values (such as integers) can be marked `Copy` (see later slides).
|
||||
|
||||
* In Rust, clones are explicit (by using `clone`).
|
||||
|
||||
</details>
|
||||
|
Loading…
x
Reference in New Issue
Block a user