Rust in Pieces

A top-down approach to learning Rust coming from Python (and vice-versa).

Introduction

This book's goal is to bring the Python and Rust developer communities closer together, and to help more developers from one language learn how to leverage the benefits of the other.

The book and accompanying code are organized into a collection of small projects, termed pieces. Each piece is a self-contained task with Python and Rust implementations that each perform the same task. The aim is to help Python developers gain familiarity with Rust, and vice-versa, by comparing and contrasting the two languages in a top-down manner.

Importantly, the pieces in this book build towards unifying Python and Rust code bases via PyO3, a highly popular open source library that allows you to call Rust bindings from Python or the Python interpreter from Rust. Using one language does not preclude using the other!

As you go through the pieces, you'll find yourself becoming proficient in writing clean, tested, production-worthy code using engineering best practices in either language, moving between them at will. Over time, you can make a more informed choice regarding when to use one or the other language for parts of a larger project.

We believe that Rust 🦀 is the among the most approachable lower-level programming languages for Python developers, and that Python is one of the most valuable high-level languages for Rust developers who are looking to build tooling for the burgeoning data, AI and ML ecosystems. The arrival of tools like PyO3 has made it highly feasible for a developer to straddle both worlds, combining their best parts, thus helping build more efficient and scalable software.

What's covered in this book?

Rust's learning curve is considerably steeper than Python's, so the table below is provided to show a mapping between each piece and its corresponding concept in Rust. As can be seen, structs, serialization, deserialization, vectors and traits are ubiquitous concepts in Rust.

PieceCategoryKey Rust concepts
Hello worldIntromacros
Data structures & constructsIntrocrates, structs, traits, implementations
Simple CSV parsingFilesserde, vec
Regex JSONFilesmatch, regex
Mock data generationFilesRNG, sampling
Age groupingFilesenums
Datetime parsingFileschrono, lifetimes
Extract pronouns from textFilesrayon, parallelism
PostgresDatabasesasync, sqlx, tokio
MeilisearchCLIsasync, async-std, clap
REST API to PostgresAPIsaxum, async, tokio
PyO3 mock data generationUnificationPyO3, Maturin
PyO3 parallel computationUnificationPyO3, Maturin