7
Reading Club: The Book Ch 4 "Understanding Ownership" [PROJECT]
(rust-book.cs.brown.edu)
A collaborative space for people to work together on learning Rust, learning about the Lemmy code base, discussing whatever confusions or difficulties we're having in these endeavours, and solving problems, including, hopefully, some contributions back to the Lemmy code base.
Rules TL;DR: Be nice, constructive, and focus on learning and working together on understanding Rust and Lemmy.
See also:
Thumbnail and banner generated by ChatGPT.
I'm not entirely sure why, but the whole
Double-Freeissue never quite sunk in from chapter 4. It's first covered, I think here, section 4.3: Fixing an Unsafe Program: Copying vs. Moving Out of a CollectionI think it was because the description of the issue kinda conflated ownership and the presence or absence of the
Copytrait, which isn't covered until way after chapter 4. Additionally, it seems that the issue mechanically comes down to whether the value of a variable is actually a pointer to a heap allocation or not (??)It was also a behaviour/issue that tripped me up in a later quiz, in an ownership recap quiz in chapter 6 where I didn't pick it up correctly.
Here's the first quiz question that touches on it (see Q2 in The Book here, by scrolling down).
Which of the following best describes the undefined behavior that could occur if this program were allowed to execute?
For those not clear, the issue, if this code were permitted to execute, is that
s2would be a pointer to the sameStringthatspoints too. Which means that when deallocations occur as the scope ends, bothsands2would be deallocated, as well as their corresponding memory allocations on the heap. The second such deallocation would then be of undefined content.I find this simple enough, but I feel like the issue can catch me whenever the code or syntax obscures that a pointer would be copied, not some other value, like in the re-cap quiz in chapter 6 that I got wrong and linked above.