Rust smartpointers for c# developer

hey guys, im really interested in going low level. I ve been programming for over 10 years now and I always used higher level languages to develop business applications. But I want to go low level and make my dream come true: game programming.

so i started reading the book, but I think it is too abstract. I am a slow learner, I need a lot of examples and try them myself.

so what are these things rust book called smart pointers ? where and when I use them ?
Box, Rc, Arc ?

This may help you get started: c++ - What is a smart pointer and when should I use one? - Stack Overflow

To appreciate what a smart pointer is it may help to understand first what a normal, i.e. non-smart, pointer is. This video may help with that: 21:Everything u need 2 know about pointers -Richard Buckland - YouTube

The above material talks about pointers and smart pointers in C++. Box, Rc etc. are just Rust equivalents of C++'s smart pointers (although they differ significantly from them because Rust differs very fundamentally from C++)

2 Likes

What book exactly are you reading?

I think the second edition does a better job and explaining concepts in proper order: Foreword - The Rust Programming Language

1 Like

exactly that book you ve linked. I think it is not enough. It doesnt give any real life examples and that what makes me learn things.

thanks for the link. i ll get back to you when im done watching

the video was really boring i didnt learn anything new but i watched other videos though. now i get the idea of smart pointers in c++-

unique_ptr -> you can only have one pointer to an adress
shared_ptr -> reference counting pointers ( when the count reaches 0, it deletes the object)

so how does rust come into play here ? i expect RC to be similar to shared_ptr ? how about others ? box, arc etc. ?

Box is analogous to unique_ptr. Arc is more analogous to shared_ptr because these refcounted pointers are threadsafe. Rc is a refcounted pointer that is for shared ownership on a single thread.

umm, what do you mean by threadsafe ?

The refcount is incremented/decremented using atomics in the case of shared_ptr/Arc - this allows shared ownership amongst threads. So the threadsafety here is with respect to the refcount maintenance being coherent/synchronized across threads.

so basically ARC for multi threaded programs and RC for single threaded ones ? and what does atomic mean ?

1 Like

Sort of, but less about singlethreaded vs multithreaded programs but more about whether you want to share ownership over heap memory across threads. You can have a multithreaded program that doesn't share ownership of a heap value amongst threads - it can just as well have an Rc for sharing ownership of a value that is never shared with other threads.

Atomics means it uses atomic types (i.e. primitives built on top of atomic operations provided by most CPUs) to toggle the refcount. See std::sync::atomic - Rust for a bit more color. But the fact it uses atomics is an implementation detail - it could just as well use a bare integer with a lock/mutex around it. That would functionally work, but would be slower performance-wise.

1 Like

oh that makes sense. thanks for the clarifications. so now i need to use these different pointers to understand them better. any examples ? tutorials ?

Rust docs have a few simple ones:

Is there something concrete you'd like to implement in Rust? That would be the best way to learn, rather than contrived examples.

Just a note though that most Rust code avoids heap allocations and prefers to work with stack values (or values embedded/"inline" in other structs). This is in contrast to C# where, besides value types/structs, all objects are heap allocated and accessed via indirection (i.e. references).

1 Like

well i would love to learn graphics programming with vulkan but then i would need to learn vulkan + rust and probably c++ as all the tutorials are for c++.

I can rewrite one of my server code written in c# but then memory management was not a concern of that program.

so basically im lost.

Just a note though that most Rust code avoids heap allocations and prefers to work with stack values (or values embedded/“inline” in other structs). This is in contrast to C# where, besides value types/structs, all objects are heap allocated and accessed via indirection (i.e. references).

is this why rust smart pointers exist ?

1 Like

I think this might be a good exercise anyway. Since you're just starting out with Rust, try to learn it by itself first - taking a project you're familiar with written in another language and reimplementing in Rust is a good approach.

Graphics programming in Rust is going to throw you into the deep end of the pool - I'd save that for later.

They exist because (a) you do need to allocate things on the heap and (b) there's no GC - you need to manage memory manually. Raw pointers require "full" management of the memory - you allocate and deallocate manually, there's no compiler support for determining when the pointer is no longer in-use. If you deallocate prematurely, you'll get use-after-free bugs (common gateway for security exploits to boot). So smart pointers integrate with the language to automate some of this and prevent a class of bugs (Rust, in particular, is very strong in this space).

The most canonical example of something using heap allocation in Rust is the Vec struct (think List in C#). The Vec manages the backing heap memory internally, but I don't think anyone would call it a "smart pointer"; that's because its purpose is much more than just managing the heap allocation (i.e. allocating it and knowing when to deallocate it) - it's a datastructure.

There are other reasons to use Box (and thus heap allocation) in Rust, such as forming owned trait objects (aka type erasure), but I suggest you hold off on learning about trait objects for now; try to understand the more fundamental aspects of Rust first.

Finally, I would continue reading the book as it'll introduce you to the concepts you'll need to know. If you don't understand something there, feel free to ask in this forum and people will happily help out.

1 Like