Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a short discussion of shadowing and ownership in ch. 04 #4048

Closed
wants to merge 1 commit into from

Conversation

chriskrycho
Copy link
Contributor

A few notes here, @carols10cents:

  • I made a no-listing number 04b because I did not want to renumber every no-listing listing in the chapter, but since it’s just the no-listing listings, I think it’s “fine” if we do, just a minor annoyance in the Git history.
  • I opted not to make a graphic for it, because it would just be a grayed out box for the first one. There, too, though, if you think that we should add a diagram, I am totally willing to adapt one of the existing ones as appropriate here—it shouldn’t be too difficult!
  • I am also still a little bit iffy on this. I landed on “yeah, this is helpful” after thinking a bunch about Mention that the left-hand side of an assignment is dropped #4001 because shadowing has some effects which can be quite surprising, and it provides some helpful scaffolding for readers to be able to build out some of those implications themselves once we introduce Drop later. (“Oh, so shadowing a variable might perform a really expensive drop operation, huh” etc.)

Comment on lines +373 to +377
We initially declare a variable `s` and bind it to a `String` with the value
`"hello"`. Then we immediately declare a new value `s`, which is a `String` with
the value `"ahoy"`. The second declaration shadows the first one, so it
immediately goes out of scope and its memory is freed. When we print the value
at the end, it will be `"ahoy, world!"`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong! I reread the Reference section on this (it had been a few years) along with a couple other relevant resources, and the String with "hello" becomes unavailable for anyone else to access, because there is no name pointing to it, but it will go out of scope at the end of the block, just like normal. (See this playground.)

This is subtle enough, and gets into enough of the details around how Drop works, that I think we should absolutely not put it here, and frankly I don’t think we should cover it explicitly in the book.

(A data point: I have been writing Rust off and on for the last 9 years, and I have never thought about this.)

Net, I am closing this, along with the original issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mention that the left-hand side of an assignment is dropped
1 participant