A macro for wrapping arithmetic.
Any code within a wrapping! { .. }
block will be transformed as follows:
a + b
becomes a.wrapping_add(b)
. Similarly for -
and *
.a += b
becomes a = a.wrapping_add(b)
. Similarly for -=
and *=
.-a
becomes a.wrapping_mul(!0)
.See this Internals thread for the motivation behind this crate.
Add this to your Cargo.toml
:
toml
wrapping_macros = "*"
```rust
fn main() { let mut sum = 0u8; for x in 0u8..50 { wrapping! { sum += x; } } } ```
This crate uses unstable APIs, and will only work on Rust Nightly.
You cannot nest another macro invocation within a wrapping!
block. For example, this will not work:
rust
let x = 41i32;
wrapping! {
println!("The answer is {}", x + 1); //~ ERROR
}
Instead, move the macro call out of the block:
rust
let x = 41i32;
println!("The answer is {}", wrapping! { x + 1 });