Rust Lang

Rust Data Types

In Rust, every value has its data type. The data type tells the compiler what kind of value it is and how to use it. Although Rust is a statically typed language, meaning the type of value should be known before compilation, Rust does an incredible job of inferring types based on the assigned value.

Data types are a universal feature among many programming languages and provide a solid building block for the programs we build with it. Keep in mind that each language has its own types which may differ from other languages.

As programmers, knowing the various primitive types provided by a language is more than crucial. Hence, for this tutorial, we will walk through the various types in the Rust programming language.

Rust Primitive Types

There are two main categories for rust primitive types:

  1. Scalar Types
  2. Compound Types

However, it doesn’t really matter whether a type is scalar or compound, all you need to know is how to use it in your programs.

Let us start off with the most common and basic types in Rust:


The first category of primitive types in Rust is integers. An integer is a number type that does not have a fractional value. There are various types of integer types in Rust and include:

  1. i8
  2. u8
  3. i16
  4. u16
  5. i32
  6. u32
  7. i64
  8. u64
  9. i128
  10. u128
  11. isize
  12. usize

Each integer type takes the format of:


The letter in this case denotes whether the number can have a sign. If the type is signed (i), it can have a negative value. If unsigned (u), the number will only be positive.

The number value represents the size in bits the type will occupy in memory. For example, an i32 value represents a 32-bit signed integer.

The isize or usize type will depend on the architecture on which the code is running. If you are on 32-bit architecture, the type will take up 32-bits and 64-bit for 64bit architectures.

NOTE: Ensure you keep your values within the range of your specified types. Otherwise, you have an overflow which may lead to dangerous operations.

The example program below shows how to declare and use integer types in Rust.

fn main() {
// integer types
let i_8 = -128;
let u_8 = 127;
let i_16 = -32768;
let u_16 = 32767;
let i_32 = -2147483648;
let u_32 = 2147483647;
let i_64 = -9223372036854775808_i64;
let u_64 = 9223372036854775807_u64;
let i_128 = -170141183460469231731687303715884105728_i128;
let u_128 = 170141183460469231731687303715884105727_u128;
let i_size = -9223372036854775808_isize;
let u_size = 9223372036854775807_usize;

println!("i_8 -> {}\nu_8 -> {}\ni_16 -> {}\nu_16 -> {}\ni_32 -> {}\nu_32 -> {}\ni_64 -> {}\ni_64 -> {}\ni_128 -> {}\nu_128 -> {}\ni_size -> {}\nu_size -> {}", i_8, u_8, i_16,u_16, i_32, u_32, i_64, u_64, i_128, u_128, i_size, u_size );

The program above shows how to declare int types in Rust. This should print:

NOTE: If you do not specify the type when assigning a numeric value, Rust will default into an i32 type.


Rust has floating-type numbers. These are numbers that include floating point values or decimal points. There are only two types of floating types in Rust: f32 and f64 that are 32-bit and 64-bit values.

Example of floating type are as shown:

let f_32 = 3.141_f32; // 32-bit floating type
let f_64 = 3.141; // 64-bit floating type

If you do not specify the type for a floating-point value, Rust will default to f64 type.


The other type provided by Rust is a Boolean. Like all Boolean logic values, it has two possible values: true or false. A Boolean is one byte in size.

Example is as shown:

let var1 = true;
let var2 = false;

Character Type

The character type refers to a single Unicode character. It is 4 bytes in size. For example, the following shows various types of Unicode char types.

let a = 'A';
let emoji = '😊';
let pi = 'π';
let great = '大';

The code examples above represent various Unicode characters.


The second category of primitive types in Rust is compound types. An array is part of compound type.

An array refers to an ordered collection of values of similar types. The code below shows an array in Rust:

let letter: [&str; 5] = ["a","b","c","d","e"];

In Rust, we declare an array using the let keyword followed by the variable name, a full-colon and the type and size inside a square bracket.


Slices are closely similar to arrays except they are dynamic. Unlike an array, a slice can grow or shrink in size.

An example is as shown:

let slc = [1,2,3,4,5];


The other type of compounds data types in Rust is a tuple. A tuple is defined a heterogenous sequence of values. A tuple can hold values of multiple types unlike an array. Tuples also have an order and can be accessed via their indexes. Check our tutorial on Rust tuples to learn more.

An example of a Rust tuple is as shown below:

let tup = (1,2,3,4,5); // no type annotation
let tup_letter: (i32, &str, f64, char) = (100, "hello", 3.141, 'A'); // explicit type annotation

In the first tuple, we let the Rust compiler to infer the type based on the values provided.

For the second one, we explicitly tell the compiler which types we want. Keep in mind that the order matters in this type of declaration.


From this tutorial, you notice Rust provides us with a suite of awesome primitive types. They allow us to handle various data types and control the data that comes in or out of our applications.

Thanks for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list