book
book copied to clipboard
Enhancement Request: Chapter 04-02 note explaining println! is special
- I have searched open and closed issues and pull requests for duplicates, using these search terms:
- chapter 04
- println!
- I have checked the latest
mainbranch to see if this has already been fixed, in this file:- it is not
URL to the section(s) of the book with this problem: https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
Description of the problem:
When learning about references and borrowing for the first time, println! is confusing as it appears to take ownership. In fact it does not, like several other macros. There is a good discussion in the answer on StackOverflow on this topic at
https://stackoverflow.com/questions/30450399/does-println-borrow-or-own-the-variable
Suggested fix:
Add a brief note box just before "Mutable References", something such as this:
It may seem as if all the println! macros we've seen so far take ownership. In fact, println! and several other standard macros implicitly use a reference for convenience. If you explicitly pass a reference the code works just the same. (Try it!)
Thanks for the note! I could go either way on this, since it’s true that there’s a slightly unusual thing going on with println! and friends; on the other hand, I am not yet persuaded that the extra text carries its weight. (It’s easy to say yes to any one specific sentence or two, but if we do that everywhere… 😬) I am going to flag this one as a question for us to come back to as we keep iterating on the book for the next edition.