4 ms·
If you want to write a linked list like you do in C, just use pointers instead of references, mark your code as unsafe, and basically you have it. I'm not sugg
by tspiteri 9y ago
If you want to write a linked list like you do in C, just use pointers instead of references, mark your code as unsafe, and basically you have it.
I'm not suggesting this is a good idea, but I'm tired of the linked list argument which kind of implies Rust is much harder than C for an equivalent linked list implementation. If you explicitly choose to lose the extra guarantees, Rust is about as easy as C to write a linked list in.
- sp332 9y agoI think I understand this. It's difficult to write concurrency-safe linked list operations in C too. This is a case where I would lean on the standard library for sure. Is there a different kind of problem you'd recommend to just dip your toes in?
- oconnor663 9y agoImplementing a singly-linked list in Rust is much easier, because you can use `Option<Box<...>>`. (That is, each node in the list can hold the unique pointer to its tail, so it's easier for the compiler to prove to itself that everything you're doing is fine.) Working through the Entirely Too Many Linked Lists book in general is super helpful, since it deliberately runs into the cases that are hard. Implementing a hash table in Rust would probably be fine as a toe dip too. It's that multiple-references-to-each-object part that really burns you in the doubly-linked list case.
- curun1r 9y agohttps://github.com/carols10cents/rustlings https://github.com/carols10cents/rustlings http://exercism.io/ http://exercism.io/ These are probably the two best sites for small, exercise training programs in Rust. Once you've gotten through a few of them, you'd probably be ready to build something useful, which might be more interesting.
- v_lisivka 9y agoImportant correction: unsafe Rust is as hard as C or C++. It means that for many developers it's just impossible to write their own linked list in Rust.
- adrianN 9y agoWriting a linked list in C is an exercise for undergraduates. I hope that almost all developers can do it with a little effort. Writing a linked list in C is not hard. Using it correctly is the hard part. In a safe language like Rust you can write your linked list using unsafe code and then build a safe interface around it.
- galangalalgol 9y agoIt isn't even a college level exercise to do linked lists in any language. The gp is probably getting at c being hard to write safe code at large scales, which I agree with, but linked lists are not large scale.