Hacker News new | ask | show | jobs
by kernelbandwidth 3439 days ago
> The very same trie code that I tried to write in Rust I wrote in one & half evenings in C++

Use unsafe. No, really, just use unsafe.

Clearly a big value of Rust is that it's a safe language, and the design patterns around safety and affine typing are emerging and good. We should want people to write and use safe code as much as possible. But the trie example seems, to me, to be very much the sort of thing unsafe is for. Writing in C++ is essentially doing everything in unsafe. Unsafe is not evil; it's an important part of the language and when it's called for, it's called for.

1 comments

I'm not sure, tries are pretty easy with safe code. It's only things like data structures with parent pointers where things get hairy.

If your goal is to use malloc and stuff (which seems to be what the author wanted) then of course you should use unsafe. Unsafe is indeed for doing abstraction-writing like this. Still, you should avoid it if you can.