2 ms·
Not sure why this was downvoted. I’m an unabashed Rust apologist and everything in here is reasonable. `as` casts are generally a code smell, but they’re somet
by stouset 4y ago
Not sure why this was downvoted. I’m an unabashed Rust apologist and everything in here is reasonable.
`as` casts are generally a code smell, but they’re sometimes the best (or only) way to accomplish a task. This can absolutely make it more challenging to identify correct and incorrect examples at a glance.
And Rust is awesome in that you can use explicit numerical operations that wrap, panic, saturate, etc on overflow. But there is no clippy lint (that I know of) to make sure you use them (which I would love).
- witcher01 4y ago> But there is no clippy lint There actually is! You can activate the lint with a `#[deny(clippy::integer_arithmetic)]` or by using the lint group `restriction`.[0] // https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic #[deny(clippy::integer_arithmetic)] fn main() { let (a, b) = (u32::MAX, 1); //let c = a + b; // clippy: `error: integer arithmetic detected` let (c, carry) = a.overflowing_add(b); // or any of checked_*, saturating_*, unchecked_*, wrapping_* println!("{a} + {b} = {c} (carry bit: {carry})"); // output: `4294967295 + 1 = 0 (carry bit: true)` } Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b595ffd91d4cf97c7a2c3fa80dd25904 https://play.rust-lang.org/?version=stable&mode=debug&editio... [0]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic https://rust-lang.github.io/rust-clippy/master/index.html#in...
- stouset 4y agoTIL, thanks!