Exploring Scalar Types in Rust: A Detailed Guide

Choose between Rust’s integer, floating-point, boolean, and character types. The examples explain type widths, literal syntax, pointer-sized integers, and Unicode scalar values.

Rust scalar types represent individual values: integers, floating-point numbers, booleans, and Unicode scalar values. This guide explains their sizes and literal syntax. If the type annotations are unfamiliar, start with the Rust introduction.

Integers

Integers in Rust come in two flavors: signed and unsigned. Each has different sizes, indicated by the number of bits they occupy in memory.

  • Unsigned Integers: u8, u16, u32, u64, u128, usize
    • These integers start with u (for unsigned) followed by the number of bits they use.
    • usize is unique as it represents the size of a pointer on the system, making it ideal for indexing arrays and vectors.
  • Signed Integers: i8, i16, i32, i64, i128, isize
    • These integers start with i (for signed) and also vary by the number of bits.

Integer Literals: Rust allows you to express integer literals in multiple formats:

  • Decimal: 98_222, 1_000_000 (underscores for readability)
  • Hexadecimal: 0xff
  • Octal: 0o77
  • Binary: 0b1111_0000
  • Byte (only for u8): b'A'

Floating-Point Numbers

Rust’s f32 and f64 types use IEEE 754 binary32 and binary64 formats. The numbers in their names are total storage widths, not the number of significant bits of precision.

  • f32: 32-bit storage, with 24 bits of significand precision
  • f64: 64-bit storage, with 53 bits of significand precision (the default floating-point type)

f64 is the default floating-point type and provides more precision than f32. Choose based on the numerical accuracy and storage needs of the application; measure performance on the target architecture rather than inferring it from pointer width. The Rust Book describes the floating-point formats.

Examples:

  • 1.0 (default f64)
  • 1.0f64 (explicit f64)
  • 1.0f32 (explicit f32)

Note: .1 is not a valid float; it must be written as 0.1.

Booleans

Booleans in Rust are represented by the bool type, which can hold one of two values: true or false. These are the only two boolean literals available in Rust, making the type straightforward and easy to use.

Example:

let is_active: bool = true;
Rust

Character Types

Rust’s char type occupies four bytes and represents a Unicode scalar value. A user-perceived character, such as an accented letter or an emoji sequence, can contain more than one scalar value.

Examples:

  • 'a' (simple character)
  • 'ℤ' (mathematical symbol)
  • '😻' (emoji)

It’s important to note that while strings in Rust are UTF-8 encoded, characters are not stored internally as char. This distinction is critical when working with strings versus individual characters.

Conclusion

Scalar types describe single values. Continue with tuples and arrays to group values, and Rust strings and UTF-8 to see why a char and a character in displayed text are not always the same thing.

Share
Prabhat Kashyap

Prabhat Kashyap

Senior Technical Architect at HCLTech · working with Leonteq Security AG

I have 10+ years of experience building distributed systems and fintech platforms. I write about practical, non-obvious engineering details that official documentation often skips.

Newsletter

Get new engineering deep dives

Scala, JVM, AI, and backend systems. I send practical articles when there is something worth reading.

Join the newsletter