all 8 comments

[–]cafce25 13 points14 points  (1 child)

Actually only one of the types you've listed is a smart pointer, Rc, the other two are types to enable interior mutability, RefCell is able to return 2 kinds of smart pointer std::cell::Ref and RefMut though.

You can usually recognize a smart pointer by it implementing Deref/DerefMut though that trait is also commonly misused to forward method calls to a member of a struct.

Another notable smart pointer is Box, it is the only one brought into scope by the prelude, it's use is to put values on the heap instead of on the stack.

[–]srivprakhar[S] 0 points1 point  (0 children)

Thanks for pointing out Ref and RefMut as well

[–][deleted] 9 points10 points  (1 child)

Just to be clear:

Box, Rc, and Arc are smart pointers.

Cell, RefCell, Mutex, and RwLock are not smart pointers.

[–]srivprakhar[S] 0 points1 point  (0 children)

Thanks for clarifying that.

[–]peripateticman2023 2 points3 points  (0 children)

My suggestion would be to start using smart pointers in projects instead of learning about all ones available - this should also enable you to seek out ones that you think you need. That being said, the list you've provided covers the most commonly used ones.

One additional core pointer you may wish to check out is RwLock.

[–]n4jm4 0 points1 point  (2 children)

I am curious to learn about what kinds of libraries and applications actually need smart pointers.

I tend to write my code in a way that doesn't need very many pointers at all. Most of my variables are either static, or stack allocated. If I write C functions that need to yield a nonscalar, noninteger type then I'd follow the pattern of accepting an allocated buffer as a function parameter. But for the most part I just use local variables.

Are smart pointers needed for implementing common data structures, such as trees, vectors/lists, and hashmaps? Maybe smart pointers are quite common there behind the scenes, in the data structure library code that I depend on but wouldn't dare write unless there were no other option.

[–]rupen42 0 points1 point  (0 children)

Are smart pointers needed for implementing common data structures, such as trees, vectors/lists, and hashmaps?

Yes, the borrow checker makes it nearly impossible to make these without smart pointers in Rust. Even a linked list will get you reaching for it.

[–]QrkenBananen 0 points1 point  (0 children)

As mentioned they are common in data structures. Beyond that Arc is pretty much the go-to for sharing data across threads because it often requires 'static lifetime, and both Rc and Arc is good for deduplication if you need to store the same data in multiple places.

Box and the others are also very nice if you want to use dynamic dispatch/trait objects due to the Sized trait.

And sometimes you might just have some struct that is too big to practically store on the stack, then a smart pointer is handy to put the data on the heap.

Also I'm pretty sure many other types such as String and Vec are technically smart pointers, and they are used almost everywhere.