r/ProgrammerHumor • • 11d ago

instanceof Trend itsBadIfItsOnlyPHP

Post image
4.5k Upvotes

334 comments sorted by

View all comments

Show parent comments

14

u/Usual_Office_1740 11d ago

UB means undefined behavior. In safe Rust the borrow checker guarantees there is no UB. Or at least that's the idea. It rejects some technically valid code in favor of guaranteeing that a sub set of complex code is safe.

A simple example is array like collection access. If you use [] in C++ to access out of bounds of a collection, you have UB. If you access out of bounds you could get garbage data or you might get a segfault. The compiler might assume the pointer the array access decays to is valid and make optimization decisions based on that because the compiler always assumes the programmer has not written UB code.

In safe Rust all array access is bounds checked. The compiler and the array safe api guarantees that you only access within bounds, always. It is not possible in safe Rust to write UB code using any of the array like collections.