4 ms·
I’m not sure. Looking through different Rust libraries, I tend to see three different styles, depending on previous programming experience. Each mimics the pr
by MereInterest 3y ago
I’m not sure. Looking through different Rust libraries, I tend to see three different styles, depending on previous programming experience. Each mimics the prior experience, with benefits.
* Everything is a struct with concrete types. This is closest to C. It benefits from the improved memory safety, without sacrificing speed.
* Everything is a Box<Rc<T>>. This is closest to dynamic garbage-collected languages, but with vastly improved performance.
* Everything is an “impl Trait”. This is closest to templated C++, but with much better ergonomics.
So, while I think I agree with your specific statement that the improved memory management may only be fully appreciated by those that cut their teeth on C’s segfaults and dangling pointers, I don’t think that’s the only benefit of Rust as a language.
- jowea 3y ago> * Everything is a Box<Rc<T>>. This is closest to dynamic garbage-collected languages, but with vastly improved performance. Wait, reference counting everything all the time is faster than normal garbage collection?
- edflsafoiewq 3y agoIt's probably slower, but unlike normal GCed languages, it's not actually anywhere close to everything.
- riku_iki 3y agoI think more importantly it is reference counting across threads, meaning mutexes and breaking memory barrier with large performance penalty.
- MrRolleyes 3y agoI believe you're discussing 'Arc'. 'Rc' is not thread safe.
- riku_iki 3y agothen this is not "closest to GC languages", since GC langs usually provide memory management with respect to concurrency.
- MrRolleyes 3y ago[dead]
- MereInterest 3y agoMany garbage collectors (e.g. CPython’s) will use reference counting as part of the process. If a reference count hits zero, the object can be destructed, prolonging the time between mark-and-sweep passes. The performance difference, though, is mainly in switching over to a compiled language in the first place.
- afdbcreid 3y ago> Everything is a Box<Rc<T>>. This is closest to dynamic garbage-collected languages, but with vastly improved performance. Why the `Box`?
- MereInterest 3y agoMostly to mimic the data structures allowed in languages where objects are held by reference. In Python, I could write a tree structure as `namedtuple(“node”, [“lhs”, “rhs”])`. If I tried to write a similar structure in Rust as `enum Node{Leaf, Branch(Node, Node)}`, the compiler rightfully complains that it would have an infinite size. But the indirection introduced by Box would let it be stored as `enum Node{Leaf, Branch(Box<Node>, Box<Node>)}`.
- jowea 3y agoI think what the previous poster is pointing out is that Box<Rc<Foo>> is an unnecessary double indirection.