Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is Rc::downgrade(this: &Self) instead of Rc::downgrade(&self)

Is there any particular reason why the type signature of Rc::downgrade doesn’t use &self and instead this: &Self as parameter?

To avoid Naming pollution when derefing? But why does Weak then use Weak::upgrade(&self) instead of Weak::upgrade(this: &Self)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To avoid Naming pollution when derefing?

Exactly. Lots of built-in containers like Rc and Box try very hard to minimize the number of inherent functions defined on them, favoring associated functions instead to allow the structure to be as transparent as possible.

Weak, on the other hand, does not implement Deref. You must consciously try to get the value from it, using upgrade, and that function can fail (a Deref impl should never fail) if the value pointed to has already been dropped.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading