Simple Data Types

Rust by example - Primitives

  • Integers: i8⮳, i16⮳, i32⮳, i64⮳, i128⮳, isize
  • Unsigned: u8⮳, u16⮳, u32⮳, u128⮳, usize
    • usize⮳ and isize⮳ are 32 or 64 bits, depending on the architecture of the computer.
  • Floating point: f32⮳, f64
  • Boolean: bool⮳: true, false
  • Char: let z: char = 'ℤ'; Unicode
  • Tuples: let tup: (i32, f64, u8) = (500, 6.4, 1);
    • Access via let five_hundred = x.0;
    • Destructuring via let (x, y, z) = tup;
  • Arrays: let a: [i32; 5] = [1, 2, 3, 4, 5]; allocated on the stack. access via let first = a[0];
    • A vector is a similar collection type provided by the standard library that is allowed to grow or shrink in size
  • Unit (aka void): ()
  • Type aliases: type Kilometers = i32;

Overflow handling

  • Wrap in all modes with the wrapping_* methods, such as wrapping_add⮳.
  • Return the std::option::Option::None⮳ value if there is overflow with the checked_* methods.
  • Return the value and a boolean indicating whether there was overflow with the overflowing_* methods.
  • Saturate at the value’s minimum or maximum values with the saturating_* methods.