3 ms·
> Rust's safety features are a godsend, but IMO easier to understand once you have experienced working without them. Passing around a pointer becomes passing
by MuffinFlavored 3y ago
> Rust's safety features are a godsend, but IMO easier to understand once you have experienced working without them.
Passing around a pointer becomes passing around a borrow or a Mutex<T> or Arc<Mutex<T>>.
Not sure how you'd summarize Rc<T> or Box<T>
- rice7th 3y agoWell, it depends on what you define by passing a pointer. Is the pointer mutable (needs to modify the data it is pointing at?) or immutable? Is it a reference ("guaranteed" pointer?) Or just a plain old pointer? (const T and mut T)? Or do you want a smart pointer, like Box<T> or a unique_ptr-ish thing with a reference counted pointer via Rc<T>? Want to pass and modify data from and to multiple threads? Yea just drop an Arc<Mutex<T>> and you're good to go. If you just want to pass some data, even around threads, without modifying it, then a plain old borrow is your best bet. Make sure to get the lifetimes right tho!
- Olreich 3y agoRc<T> is a chunk of memory you can only access through special versions of malloc and free. These don’t actually allocate new memory with the OS though, they just give you pointers to the memory and tell the memory management system that you’re done using it. Once everyone who has allocated that memory calls free, then the memory is actually given back to the OS. Box<T> is the same, except if you have multiple people try to call the special malloc it will fail to compile.