Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

integer

Integer types and operations.

This module provides the built-in integer types and their associated operations.

Integer Types

The following integer types are available:

  • Unsigned integers: u8, u16, u32, u64, u128, u256
  • Signed integers: i8, i16, i32, i64, i128

Operations

Integer types implement various traits that enable common operations:

  • Basic arithmetic via Add, Sub, Mul, Div, Rem and DivRem
  • Bitwise operations via BitAnd, BitOr, BitXor, and BitNot
  • Comparison via PartialEq and PartialOrd
  • Safe arithmetic via CheckedAdd, CheckedSub, CheckedMul
  • Wrapping arithmetic via WrappingAdd, WrappingSub, WrappingMul
  • Overflow handling via OverflowingAdd, OverflowingSub, OverflowingMul

Examples

Basic operators:

let a: u8 = 5;
let b: u8 = 10;
assert_eq!(a + b, 15);
assert_eq!(a * b, 50);
assert_eq!(a & b, 0);
assert!(a < b);

Checked operations:

use core::num::traits::{CheckedAdd, Bounded};

let max: u8 = Bounded::MAX;
assert!(max.checked_add(1_u8).is_none());

Conversions

Integers can be converted between different types using:

  • TryInto for potentially fallible conversions
  • Into for infallible conversions to wider types

Fully qualified path: core::integer

Enums

Extern functions