-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Description
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
Boxchapter 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 thatBoximplements theDisplaytrait 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
Derefchapter say:Rust does deref coercion when it finds types and trait implementations in three cases:
- From
&Tto&UwhenT: Deref<Target=U>. - ...
I was unclear how this rule would deref
*my_favorite_songfromMp3into aVec, becausemy_favorite_songis 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. - From