4 ms·
I think you've covered all the major options: - Use Rc (or Arc if you need to be thread-safe) - Use a Vec, slotmap, or a similar data-structure and store hand
by nicoburns 4y ago
I think you've covered all the major options:
- Use Rc (or Arc if you need to be thread-safe)
- Use a Vec, slotmap, or a similar data-structure and store handles (indexes). This doesn't entirely prevent use-after-free bugs. But it does tend to make them panics rather than silent errors.
- Use unsafe (and ideally create your own higher-level safe abstraction)
In theory, there should also be 4:
- Use a GC library
But I haven't seen a good implementation of this yet.
---
If you're missing something, it's perhaps that most lifetimes aren't dynamic. So you get all the benefits of Rust's ownership in the 80% case. And in the 20% case you don't get any particular benefits, but you still have all the same options available to you as in other languages.
- cyber_kinetist 4y agoBut most of the bugs I often face and seem to struggle with are with dynamic lifetimes (in graphics / simulation / gamedev). So even as a relatively low-level systems dev I don’t really find Rust’s borrow checker that useful.
- insanitybit 4y agoI think there are cases where borrow checking is difficult for a domain, but without a lot more detail it's hard to say. My understanding is that graphics/gamedev is one of those cases and if you aren't willing to use ECS you may have problems.
- cyber_kinetist 4y agoI am personally using generational indices for a lot of my stuff, no smart pointers. Many ECS implementations also seem to use this technique. But if you're stuck with legacy code in $WORK you are stuck with raw/smart pointers and these really have lots of issues.
- nicoburns 4y agoI guess the benefit of Rust in this case is that you will get an index out of bounds error rather than Undefined Behaviour.
- cyber_kinetist 4y agoYou can also check index out of bounds in C++ code, and you actually have more freedom to control it in C++. In Rust bounds checks are required unless you use unsafe, in C++ you can turn it on/off freely depending on your usecase (for example, when you are in debug mode vs. deploying your code).