-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Description
First of all, I love the book so far! It is clear and concise, which ultimately helped in me catching a potential inconsistency. Please correct me if I'm wrong!
The paragraph in question is one of the later ones, under the subtitle "Return Values and Scope". I quote:
"The ownership of a variable follows the same pattern every time: Assigning a value to another variable moves it. When a variable that includes data on the heap goes out of scope, the value will be cleaned up by
dropunless ownership of the data has been moved to another variable."
The current wording suggests that move semantics apply to every assignment; however, the previous code example explicitly demonstrates that ´i32´ types are copied rather than moved.
fn main() {
let s = String::from("hello"); // s comes into scope
takes_ownership(s); // s's value moves into the function...
// ... and so is no longer valid here
let x = 5; // x comes into scope
makes_copy(x); // Because i32 implements the Copy trait,
// x does NOT move into the function,
// so it's okay to use x afterward.
} // Here, x goes out of scope, then s. However, because s's value was moved,
// nothing special happens.
Have I understood it correctly that there is a contradiction there? A proper rephrasing could in that case be something like:
"The ownership of a variable follows the same pattern every time: assigning a value to another variable moves it (unless the type implements the Copy trait). When a variable that includes data on the heap goes out of scope, the value will be cleaned up by drop unless ownership of the data has been moved to another variable."
While I understand the book introduces different concepts at different stages, the concept of the Copy trait was introduced just prior to this section. Because the text says it follows this pattern "every time," it seems to contradict the behavior of Copy types just established.