3 ms·
"unsafe" doesn't always mean unsafe. For example, you wish to implement a Vector in Rust. You generally don't want to allocate that Vector over and over, so y
by bsder 5y ago
"unsafe" doesn't always mean unsafe.
For example, you wish to implement a Vector in Rust.
You generally don't want to allocate that Vector over and over, so you request more memory than you need.
That "extra" memory is uninitialized which is a big no-no in Rust. You might be able to initialize that memory, but that will have performance implications that C/C++ programmers would just laugh at.
However, your "invariant" is that you never access that memory until you have a real element that you put there. That's perfectly fine.
Nevertheless, accessing that uninitialized area will always be "unsafe" from the Rust compiler point of view.