Skip to content

Instantly share code, notes, and snippets.

@ibrahim-eshag
Last active September 4, 2025 14:43
Show Gist options
  • Select an option

  • Save ibrahim-eshag/60c24769197efc0e8e0bb72d993d451d to your computer and use it in GitHub Desktop.

Select an option

Save ibrahim-eshag/60c24769197efc0e8e0bb72d993d451d to your computer and use it in GitHub Desktop.
Rust notes
// - Overflow:
fn interproduct(a: i32, b: i32, c: i32) -> i32 {
return a * b + b * c + c * a;
}
fn main() {
println!("result: {}", interproduct(120, 100, 248));
}
/*
Arithmetic operations in rust is very similar to other languages, with similar precedence.
What about integer overflow?
In C and C++ overflow of signed integers is actually undefined, and might do unknown things at runtime. In
Rust, it’s defined.
Change the i32’s to i16 to see an integer overflow, which panics (checked) in a debug build and wraps in a release
build. There are other options, such as overflowing, saturating, and carrying.
These are accessed with method syntax, e.g., (a * b).saturating_add(b * c).saturating_add(c * a).
*/
// - Crrying in Rust:
/*
Carrying in Rust refers to a special kind of arithmetic operation where, if an operation (like addition or multiplication)
produces a result that overflows the type’s maximum value, the operation returns both the wrapped result and a carry flag
indicating that overflow occurred.
*/
let a: u8 = 30;
let b: u8 = 10;
let (result, carry) = a.overflowing_mul(b);
// result == 44 (because 30 * 10 = 300, and u8 wraps at 256: 300 - 256 = 44)
// carry == true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment