3 ms·
If you want it safe, then the C++ version should also add a (single) length check. To fix rust's multiple points of failure, move the check to the top, by add s
by labawi 5y ago
If you want it safe, then the C++ version should also add a (single) length check. To fix rust's multiple points of failure, move the check to the top, by add something like 'let length_check = slice[5]', moving the 'slice[5]=5' to the top (or something nicer, don't know rust), and you will get only a single point of failure and a single check, same as safe C++ with matching API.
If you want to have 0 runtime checks, like the C++ example, you can
1) make it unsafe and actively dangerous, like the C++ version
2) change the API so minimum length is guaranteed (probably possible in rust, harder in C++)
3) have the function private/inline where sufficient length is known/can be deduced and the check will be omitted
- expnkx 5y agoIf you want safety. you can just use _GLIBCXX_ASSERTIONS https://godbolt.org/z/eThdeW3Mv https://godbolt.org/z/eThdeW3Mv It gives you all bounds checking you need. In fact, a lot of Linux distributions (like Fedora) built all packages with _GLIBCXX_ASSERTIONS by default.