From d4c08e6c9d327190d566a036aef2eb39db61615c Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Tue, 18 Jul 2023 17:52:26 +0200 Subject: [PATCH] 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 * Apply suggestions from code review Co-authored-by: Dustin J. Mitchell --------- Co-authored-by: Dustin J. Mitchell --- src/ownership/move-semantics.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ownership/move-semantics.md b/src/ownership/move-semantics.md index 5bdb8c7c..ecea9163 100644 --- a/src/ownership/move-semantics.md +++ b/src/ownership/move-semantics.md @@ -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`).