←back to thread

611 points LorenDB | 1 comments | | HN request time: 0s | source
Show context
codedokode ◴[] No.43908897[source]
What about catching integer overflow? Free open-source languages still cannot do it unlike they commercial competitors like Swift?
replies(5): >>43908922 #>>43909326 #>>43909444 #>>43910683 #>>43912986 #
ultimaweapon ◴[] No.43912986[source]
Rust is the only language I can easily control how integer overflow should behave. I can use `var1.wrapping_add(var2)` if I want the result to be wrapped or `var1.checked_add(var2)` if I don't want it to overflow.
replies(1): >>43913695 #
codedokode ◴[] No.43913695[source]
The functions are so verbose and inconvenient that even Rust developers themselves do not use them. For example, in this code [1] they used a wrapping addition instead of "checked_add" because it is faster to write.

For comparison, Swift uses "+" for checked addition and as a result, majority of developers use checked addition by default. And in Rust due to its poor design choices most developers use wrapping addition even where a checked addition should be used.

[1] https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#2010

replies(2): >>43913890 #>>43917535 #
ultimaweapon ◴[] No.43913890{3}[source]
Checked addition by default will have too much overhead and it will hurt performance, which unacceptable in Rust since it was designed as a system language. Swift can use checked add by default since it was designed for application software.

Your example code is not because it is faster to write, it is because it is impossible for its to overflow on that line.

replies(1): >>43918595 #
1. codedokode ◴[] No.43918595{4}[source]
Why should checked addition have any overhead? You should just use checked addition instruction (on architectures that support it) instead of wrapping addition.

Or just because on Intel CPUs it has overhead, we must forget about writing safer code?