3 ms·
Rc, Cell & RefCell are suppose to be rare. For example I've got 2,000 line Rust program in front of me and I've used Arc 3 times and RWLock 1 time, that's all.
by ReflectedImage 4y ago
Rc, Cell & RefCell are suppose to be rare. For example I've got 2,000 line Rust program in front of me and I've used Arc 3 times and RWLock 1 time, that's all.
You need to structure your program as a Directed Acyclic Graph (DAG), with things interacting only with the things below them in the graph.
Then occasionally you might need to break the DAG structure by using Rc, Cell & RefCell, etc...
- scotty79 4y agoThe thing is not everything can be expressed as a DAG. And finding it towards the end of writing your program after hours of fighting with borrow checker is extremely unpleasant. And I don't think I ever landed in the situation where I could fix the discrepancy by sprinkling in few Rc, RefCells and such. So I prefer to write with RefCells from the start and when I got the thing working and I am ambitious enough then I look at which parts could be borrows instead and I swap them out.
- ReflectedImage 4y agoThere are many many many ways you can express something and it's very likely that one of those ways is a DAG. The issue here is that you are writing C++ code rather than Rust code.
- scotty79 4y agoHow do you express as a DAG a tree where nodes need to keep references to their children and parents? Two separate synced trees? Is it worth it? > The issue here is that you are writing C++ code rather than Rust code. How dare you! I'm writing TypeScript code! ;-) Rust is not Forth. I can write whatever I want and there's nothing wrong with that.
- ReflectedImage 4y ago> How do you express as a DAG a tree where nodes need to keep references to their children and parents? Rewrite your program in a form where it does not contain a tree. If you want an actual tree as a data structure, see the trees crate. > Rust is not Forth. I can write whatever I want and there's nothing wrong with that. And other people write Haskell code in Python :p. If your code style doesn't match the language you are using you are going to have a lot of unnecessary friction.
- scotty79 4y agoI think Rust is flexible enough to still work very well with my style. But you inspired me about something. I think I can rewrite the program that I am writing to use reverse Polish notation instead of a tree. Thanks!
- ReflectedImage 4y agoGood luck!