3 ms·
Yeah see, that is what I thought at first as well. I sort of imagined that I could get the best of both world, and just "unsafe { <C style code> }" my way out
by Galanwe 3y ago
Yeah see, that is what I thought at first as well.
I sort of imagined that I could get the best of both world, and just "unsafe { <C style code> }" my way out for performance critical things.
But the thing is, the static safety boundaries of Rust allow the compiler to make much, much tighter assumptions than C & C++, especially around aliasing rules, un initialized memory, and moves.
When you relax these boundaries with "unsafe {}", you don't enter "C world", you enter the litteral gates of hell where any innocent temporary cast can throw you in a random load/store reordering bug.
- stouset 3y agoThanks for the reply. I think if you combine unsafe with raw pointers instead of borrows you relax the rules enough to avoid this. But in this specific corner of things, I haven’t had direct experience so you may very well be right. There are still some Rust-specific details you would still need to handle—as you mentioned, uninitialized memory—but for that one specifically I haven’t found MaybeUninit to be particularly cumbersome.
- iudqnolq 3y agoDo you mean innocent temporary cast from a pointer to a reference? Cause yeah, those aren't innocent. And that's a newer realization and there were serious documentation issues around it.