4 ms·
what kind of projects will one want to write in rust? I am going through rust right now , and I find it very difficult, simple stuff like doubly linked list or
by bulldoa 9y ago
what kind of projects will one want to write in rust? I am going through rust right now , and I find it very difficult, simple stuff like doubly linked list or graph is not very intuitive, especially when you want to update inside rc using refcell.
Any tips for newbie like me on motivation and learning?
- steveklabnik 9y agoWhat's easy and hard in Rust isn't the same as what's easy and hard in other languages. As a beginner, don't try to learn Rust by implementing data structures, as you need to know a lot of Rust to do it correctly. Here's an entire book on writing linked lists, for example: http://cglab.ca/~abeinges/blah/too-many-lists/book/ http://cglab.ca/~abeinges/blah/too-many-lists/book/ What did you do so far to learn? How do you learn other languages, and what other languages do you know?
- michaelchisari 9y agoI'm happy to hear that my struggles with doing data structures in Rust were not my own specific failings. A tiny bit of vindication there.
- bulldoa 9y agoI read the rust tutorial up to chap 16, currently reading too-many-list and have not read nomicon, I know python, javascript, golang pretty well. And I know a bit of java and scala. 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.
- steveklabnik 9y agoGotcha, thanks. Based on what you know, I'd say pointers and stuff like memory layout are generally going to be where you have issues, and data structures are basically an exercise in applied pointers and memory layout. So you shouldn't feel bad! Regarding what you should do next, reading the last few chapters of the book (19 in particular will be useful for you) would be good; and chapter 20 is a project, which it seems like you are looking for. I can also highly recommend the new O'Reilly book as well. > 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.
- stjepang 9y agoBut... doubly-linked lists and graphs are easy in Rust! :) Here are a few examples. Doubly-linked list: https://github.com/stjepang/vec-arena/blob/master/examples/linked_list.rs https://github.com/stjepang/vec-arena/blob/master/examples/l... Splay tree: https://github.com/stjepang/vec-arena/blob/master/examples/splay_tree.rs https://github.com/stjepang/vec-arena/blob/master/examples/s...
- flukus 9y agoCorrect me if I'm wrong, but reading through the linked list code it seems as though it's just a linked list structure on top of a normal vector (via the arena class), combining the worst aspects of each.
- stjepang 9y agoYes, it is. But why 'the worst' aspects of each, though? I see it as the opposite: linked lists built on top of vectors combine good cache efficiency of vectors with algorithmic benefits of linked lists (many operations become O(1), or at least in the amortized sense). 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.
- flukus 9y ago> Yes, it is. But why 'the worst' aspects of each, though? I see it as the opposite: linked lists built on top of vectors combine good cache efficiency of vectors with algorithmic benefits of linked lists (many operations become O(1), or at least in the amortized sense). 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.
- leshow 9y agoI find graphs and trees are common 'exercises' that people do when they try out a new language, but you usually won't be implementing stuff like this day to day. You'd be using a well tested library, not cooking one up yourself. My advice would be to try and write some 'real world' code instead of programming kattas to get a feel for the language.
- bulldoa 9y agoyeah, but another thing I am worried is that if I want to get a rust job, I should be able to do interview problems in rust, which mostly consist of linked list, tree and graph.
- mmstick 9y agoI landed a job w/ Rust, and the programming challenge I was given was a simple C programming challenge to implement a parser to parse gzip headers, to which I did it in both C and Rust. Nobody hiring for a position that's largely Rust is going to ask you to write linked lists, trees, and graphs. They're going to want you to build something more complex & practical.
- steveklabnik 9y agoI would argue that's a terrible interview, as anyone who's in the position to give a Rust job should know this about Rust.
- baq 9y agoyou'd use a cell/refcell/box or just wouldn't get this excercise as everyone who got past it in rust know it's not easy and critically it's not supposed to be easy. it's difficult in rust because recurrent data structures are super hard to reason about in general in terms of borrowing and ownership when AOT compiling and you unfortunately must know this to be efficient at rust; this realization is one of the stages of learning the language.
- deleted 9y ago[deleted]