-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Smart Pointer dereferencing rules #538
Comments
Thanks @joliss ! Both great points. We should explicitly cover both of these things, IMO. |
In the section on interior mutability under the explanation for listing 15-15, it's mentioned that you can edit the value in an let value = Rc::new(RefCell::new(5));
// ...
*value.borrow_mut() += 10; The explanation seems to overemphasize the precedence of the deref operator - rather, we're calling // behind the scenes
*(*value).borrow_mut() += 10; Just a small issue with semantics, but would fall under issue 1 of @joliss's post. |
So @steveklabnik and I talked about this today, sorry this has taken us so long to get to!! We made this clarification around this example involving dereferencing We decided the Regarding the first part:
Yes, we do need explicit dereferencing to get a
Your intuition is also correct here-- the implementation of
I'm not sure where your intuition is incorrect here! As @steveklabnik and I were trying to figure out how to address this, we came to a conclusion that this is the interaction of a bunch of features (method auto dereferencing, deref coercions, smart pointers) and we've addressed each of these separately, but not this combination explicitly. There are a lot of features that we don't have the space to show how they interact exactly, but we do want to build up the readers' intuitions of the features in isolation so that using them together makes sense. We're just not sure exactly what to do in this case, especially because you do have correct intuitions! We're going to close this issue for now, please open new issues if you have a chance to read over the edits to chapter 15 made in #898 and have further thoughts ❤️ |
I'm reading the Smart Pointers chapter, and I found myself feeling confused about two things for quite a while:
I was first left wondering why we can use a Box without dereferencing it in the
Box
chapter inprintln!("b = {}", b);
. Then I thought that the automatic (de)referencing rules ("Where's the->
operator?", Chapter 5) might apply. Then I tried passing aBox<i64>
into ani64 -> ()
function, which failed, so I finally concluded that we do need explicit dereferencing (*b
), except thatBox
implements theDisplay
trait so we can pass it directly intoprintln!
.So the problem I'm reporting here is that I ended up with the wrong intuition upon reading the examples. Perhaps being more explicit about what's going might help.
Also, I wonder if it might be a good idea to link to the automatic (de)referencing rules somewhere in the Smart Pointers chapter as a reminder, because surely they are relevant here.
The deref coercion rules at the bottom of the
Deref
chapter say:I was unclear how this rule would deref
*my_favorite_song
fromMp3
into aVec
, becausemy_favorite_song
is of typeMp3
(T
) and not&Mp3
(&T
) -- literally until I wrote this issue, and then I realized that Rust probably automatically inserts an&
to make it work, per the same automatic (de)referencing rule as above. This might be worth pointing out explicitly, if that's what's actually hapenning.The text was updated successfully, but these errors were encountered: