What did you do so far to learn? How do you learn other languages, and what other languages do you know?
What did you do so far to learn? How do you learn other languages, and what other languages do you know?
Usually I learn language by implementing data structures: linked list, tree, graph.
If I can do those then I am pretty confident in doing mostly everything else.
Rust is really the first language that I have trouble with writing simple data structure, I have a bit of issue with scala on covariant and contravariant generics and monads, but they are not bad after I learned it.
For rust everything is awesome until I get to pointers since I have to wrestle with nested smart pointer that makes it insanely hard to manipulate, the rc, refcell nesting is just not very ergonomic (maybe once I learn unsafe it will be easier?).
I like rust paradigm alot since it brings safety and speed, so I was wondering if I am missing anything since I am having some trouble.
> I was wondering if I am missing anything
Nope, I think it's just that your experience led you astray.
If I need a linked list in Rust, I... well, I don't ever use linked lists (you can read the book for why). If I had to, I'd use the one in the stdlib.
If I need a graph in Rust, I use petgraph from crates.io.
Basically, other people are good at writing data structures, so I use their work as much as possible. Only if I'm particularly interested in a specific structure, or I require one that doesn't have some implementation out there yet, do I try to write my own.
Of course, at some point you'll want to learn it, but it's just going to be frustrating until it clicks. The less frustrated you are with the rest of Rust when that happens, the less overall frustration you'll have.
> (maybe once I learn unsafe it will be easier?)
That's very possible. Often, you can take a C implementation and translate it into unsafe Rust. Many data structures use unsafe internally, and present a safe interface.
Good luck on your journey! I hope it all makes sense for you soon.
Doubly-linked list: https://github.com/stjepang/vec-arena/blob/master/examples/l...
Splay tree: https://github.com/stjepang/vec-arena/blob/master/examples/s...
Of course, arenas do have tradeoffs - I'll try to summarize.
Pros:
- Completely safe.
- More ergonomic than dealing with `Rc<RefCell<T>>`.
- Improved cache efficiency of linked data structures.
- Fast node allocation and deallocation.
Cons:
- Any two simultaneous node accesses must be immutable.
- Accessing a node incurs the cost of a bound and liveness check.
- Vector growth can be costly and cause a spike in latency.
- Vector growth moves all nodes in memory.
- Removing a node creates a hole in the vector, possibly wasting memory.
Generally speaking, I think anyone learning Rust and trying to implement a graph data structure should first do it on top of a vector. It's easier than other commonly suggested approaches and it's not a bad one either. In fact, that's how rustc builds some of its data structures, how conrod (a GUI toolkit) represents its widget graph, and how Way Cooler (a window manager) manages window tilings.
That's really going to depend on the size of the list, if it's larger than the cache size then it's not as quick to iterate over as a vector because it's jumping around to random (albeit contiguous) memory.
> Generally speaking, I think anyone learning Rust and trying to implement a graph data structure should first do it on top of a vector.
That really depends on why your learning it. Typically when I'm learning/evaluating a new language I run through pain points like this because it show you the good and the bad.