- I have searched open and closed issues and pull requests for duplicates, using these search terms:
- drop
- dropped
- assign
- assigned
- assignment
- I have checked the latest
main branch to see if this has already been fixed, in these files:
URL to the section(s) of the book with this problem: see above
Description of the problem:
- The book explains that a value is dropped, when its owner goes out of scope (in the Ownership Rules section of chapter 4.1), but it does not explain that they are also dropped when its owner gets assigned a different value. These are the currently listed rules:
|
### Ownership Rules |
|
|
|
First, let’s take a look at the ownership rules. Keep these rules in mind as we |
|
work through the examples that illustrate them: |
|
|
|
* Each value in Rust has an *owner*. |
|
* There can only be one owner at a time. |
|
* When the owner goes out of scope, the value will be dropped. |
- Chapter 15.3 explains the
Drop trait (and shows the reverse drop order of multiple let bindings with some example code), but also does not mention the effect of an assignment.
- To find out about this behavior or to find out when (if at all) values that are overwritten via assignment are dropped, one currently has to look into the reference chapter for destructors (version at issue creation), it states:
Assignment also runs the destructor of its left-hand operand, if it's initialized.
Suggested fix:
-
In chapter 4.1, mention that a reassignment of the owner of a value also drops that value.
For example, the last bullet point of the Ownership Rules may be replaced with (added parts marked):
- When the owner goes out of scope or is assigned a different value, the (original) value will be dropped.
Or another rule may be added:
- When the owner is assigned a different value, the original value will be dropped.
-
In chapter 4.1, add an explaining example of the behavior, for example between these two paragraphs. The example might look like this:
{
let mut s = String::from("hello");
// do stuff with s
s = String::from("world"); // s gets reassigned, so the "hello" String is dropped here
// do stuff with s
} // this scope is now over, and the "world" String will be dropped
-
In chapter 15.3, add an example that shows the drop behavior of an assignment, by using the CustomSmartPointer already defined in the chapter. Alternatively, extend the first example in the chapter with the assignment behavior (might be too much in a single example?).
mainbranch to see if this has already been fixed, in these files:URL to the section(s) of the book with this problem: see above
Description of the problem:
book/src/ch04-01-what-is-ownership.md
Lines 86 to 93 in 04bc139
Droptrait (and shows the reverse drop order of multipleletbindings with some example code), but also does not mention the effect of an assignment.Suggested fix:
In chapter 4.1, mention that a reassignment of the owner of a value also drops that value.
For example, the last bullet point of the Ownership Rules may be replaced with (added parts marked):
Or another rule may be added:
In chapter 4.1, add an explaining example of the behavior, for example between these two paragraphs. The example might look like this:
In chapter 15.3, add an example that shows the drop behavior of an assignment, by using the
CustomSmartPointeralready defined in the chapter. Alternatively, extend the first example in the chapter with the assignment behavior (might be too much in a single example?).