Re:The borrowchecker is what I like the least about Rust
by aapoalas on Saturday July 19, 2025 @04:46PM
(#44619226)
If you start making assumptions based on indices, you can turn logical errors into memory safety errors. ie. whenever you use unsafe with the SAFETY comment above it mentioning an index, you'd better be damn sure that index is valid.
This goes for not only unchecked indexing but also eg. transmuting based on a checked index into a &[u8] or such. If those indexes move in and out of your API and you do some kind of GC on your arrays / vectors, then you might run into indices being use-after-free and now those SAFETY comments that previously felt pretty obvious, even trivial, may no longer be quite so safe to be around of.
I've actually written about this previously w.r.t. the borrow checker and implementing a GC system based on indices / handles. My opinion was that unless you're putting in ironclad lifetimes on your indices, all assumptions based on indices must be always checked before use.
Re:The borrowchecker is what I like the least about Rust
by _dain_ on Saturday July 19, 2025 @04:53PM
(#44619287)
My comment was implicitly about safe Rust. Obviously if you're using `unsafe`, you have to deal with unsafety ...
Re:The borrowchecker is what I like the least about Rust
by ameliaquining on Saturday July 19, 2025 @09:35PM
(#44621126)
Different meanings of "unsafety". A cool thing about Rust's references, when used idiomatically, is that not only are you guaranteed no memory corruption, you're also guaranteed no runtime panics. These are not "unsafe" by Rust's definition, but they're still a correctness violation and it's good to prevent them statically when you can. Indices don't give you this protection.
Re:The borrowchecker is what I like the least about Rust
by AquilaFasciata on Monday July 21, 2025 @10:32AM
(#44635570)
Reminder that this is what the author said about safety: "it manages to combine speed and low-level control with a high level of bug-resistance, more commonly referred to as safety"
Indices aren't simply "references but worse". There are some advantages:
- they are human readable
- they are just data, so can be trivially serialized/deserialized and retain their meaning
- you can make them smaller than 64 bits, saving memory and letting you keep more in cache
Also I don't see how they're unsafe. The array accesses are still bounds-checked and type-checked. Logical errors, sure I can see that. But where's the unsafety?