3 ms·
Doubly linked lists are impossible in safe rust by definition since the only safe way to modify a memory location is via a &mut reference. And Rust guarantees t
by dmitrygr 9y ago
Doubly linked lists are impossible in safe rust by definition since the only safe way to modify a memory location is via a &mut reference. And Rust guarantees that that any memory location can only be pointed to by only one &mut reference. A doubly linked list by definition has two pointers pointing to each node, so no, your example will not work.
- steveklabnik 9y agoThe example uses raw pointers, which aren't safe Rust. Note the *mut, not &mut. It's the direct translation of your C, which is why I mentioned that specifically.
- dmitrygr 9y agoIf it has no protections then why even use rust? You cannot both claim safety and then claim versatility by abandoning said safety. Because without your claimed safety, I might as well use C and not do battle with my compiler.
- eropple 9y agoThe claims of safety and versatility are not in opposition. There are cases where the compiler can't prove what you're doing is safe, so take the guards off and do it yourself. In the 99% case (I've literally never written unsafe Rust), you leverage its tooling to better communicate intent and safety. Clinging to an exceptional case and pretending it's the rule doesn't hold in practice.
- dmitrygr 9y agoThe OP i was arguing against said if something is hard to do in Rust it's probably an anti-pattern with respect to memory performance or safety. To counter a statement of the form "nothing of type X is good", only a single example is needed. I provided it.
- dragonwriter 9y ago> To counter a statement of the form "nothing of type X is good", only a single example is needed. “If something is hard in Rust, it is probably an anti-pattern” (emphasis added) is not equivalent to a statement of the form “nothing of type X is good”. It is equivalent to the form “most things of type X are not good”, which cannot be rebutted by a single example.