An In-Depth Guide to Data Types in Rust

Understanding Rust Data Types

Yash
3 min readJan 6, 2024
By author

In my previous-post, I provided a high-level introduction to Rust - an innovative systems programming language that offers speed, safety, and concurrency. As a data scientist well-versed in Python, understanding how Rust’s data types map to Python is key for me to effectively work with Rust.

Here's what we'll cover:

  • An overview of data types in Rust
  • Numeric types
    - Integers
    - Floating point
  • Boolean, character, and compound types:
    - Bool
    - Char
    - Tuples
    - Arrays
    - Vectors
    - Strings
    - Structs
    - Enums

With my background in data science and Python, I'll be exploring how Rust's powerful type system maps to the types I use daily in Python. This will equip any Python developer to leverage Rust's speed, safety and concurrency.

Let's dive in to learning Rust's robust data types!

An Overview of Data Types in Rust

Rust comes equipped with a variety of data types that fall into two main categories:

Primitive Types

The primitive types in Rust provide basic building blocks like numbers, characters, and booleans. They include:

- Integers (signed and unsigned)
- Floating point numbers
- Characters
- Booleans

These will feel familiar if you've worked with other languages before.

Compound Types

Beyond the primitives, Rust offers several compound or complex data types for building more complex structures:

- Tuples
- Arrays
- Vectors
- Strings
- Structs
- Enums

These allow Rust to model data in a very flexible and customized way.

Now let's explore each of these data types in Rust and see how they compare to Python.

Numeric Types

Integers

Rust integers come in two flavors - signed and unsigned. The key types are:

// Signed 
let x: i8 = -5;
let y: i32 = 50;

// Unsigned
let z: u16 = 255;
  • Signed - i8, i16, i32, i64, i128 (can be positive or negative)
  • Unsigned - u8, u16, u32, u64, u128 (only positive)

The number indicates the bit width. For example, i32 is a 32 bit signed integer.

In Python, integers have unlimited precision by default. To get fixed-width integers in Python, you need to use the `int` class from the `ctypes` module like `int8` or `uint32`.

Floating Point

Rust provides two floating point types - `f32` for 32 bit floats and `f64` for 64 bit floats. For example:

let x = 2.5; // f64 
let y: f32 = 3.14;

Python similarly has a `float` class that is 64 bit by default, like Rust's `f64`.

Boolean, Character and Compound Types

Now let's quickly cover the remaining Rust data types and their Python equivalents:

- bool:

Just like Python’s “bool” for true/false values.

let t = true;
let f: bool = false;

- char :

A Unicode character type similar to a single character Python “str”.

let a = "a";
let name = "rohit";

- Tuples :

Fixed length sequence, similar to Python tuples.

let tup = (500, 6.4, 1);

- Arrays :

Fixed length collection of the same type, similar to a Python list.

let a = [1, 2, 3];

- Vectors:

Growable sequence like Python lists.

let mut v = Vec::new();
v.push(1);
v.push(2);

- Strings:

UTF-8 encoded text, similar to Python string but growable.

let greeting = "Hello world!";

- Structs and Enums:

Custom data types, similar to Python classes.

struct Person {
name: String,
age: u8
}
enum Color {
Red,
Green,
Blue
}

So in summary, Rust's data types have a lot of overlap with Python's but with Rust's own unique capabilities and focus on performance.

Mastering Rust’s data types will equip you to build fast and safe data-oriented programs!

I hope this gave you a helpful intro to Rust’s data types. Let me know if you have any questions!

I’m still learning Rust myself, so we can dive into this together..🤜🤛..Looking forward to sharing more about Rust with you soon…😎

Next Arcticle on Rust:

Click ➡️ subscribe if you want to see more content like this, your support 🫶 fuels me to keep writing! 🌟🧑‍💻🚀

--

--

Yash

I'm a Data Scientist & Renewable Energy geek 🌱 Exploring Data📊, Green tech🌍, and Innovation💡 Hope to write on Data Science, Life, & Everything in between ;)