Leanpub Header

Skip to main content

Refactor Python with Rust

From Duck Typing to Zero-Cost Abstractions

Your Python is elegant, expressive, and — let's be honest — slow where it counts. This book teaches you to rewrite the 3% that hurts in Rust, without rewriting the 97% that doesn't, by building a real log intelligence platform from first spark to production-ready package. If you've ever stared at a profiler's output and thought there has to be a faster way — there is, and you won't even need to give up pip install.

Minimum price

$14.99

$24.99

You pay

$24.99

Author earns

$19.99
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.

Buying multiple copies for your team? See below for a discount!

PDF
EPUB
WEB
About

About

About the Book

Most Python performance books hand you a list of tricks. This one hands you a second language. Python and Rust follows the construction of Pyre, a log intelligence platform that begins as a pure-Python prototype and evolves, chapter by chapter, into a hybrid system with a Rust core — fast enough to chew through millions of log lines without breaking a sweat. Along the way, you'll learn to wield PyO3 and maturin, drop the GIL for true parallelism, bridge asyncio and Tokio, design Rust types that feel native from the Python side, and ship the whole thing as a single pip install. The book assumes you're already dangerous in Python and have survived your first encounter with the borrow checker; what it teaches is the hard, interesting part — making two very opinionated languages collaborate on production software without losing your mind, your test coverage, or your weekends.

Team Discounts

Team Discounts

Get a team discount on this book!

  • Up to 3 members

    Minimum price
    $47.00
    Suggested price
    $72.00
  • Up to 5 members

    Minimum price
    $76.00
    Suggested price
    $116.00
  • Up to 10 members

    Minimum price
    $133.00
    Suggested price
    $203.00
  • Up to 15 members

    Minimum price
    $190.00
    Suggested price
    $290.00
  • Up to 25 members

    Minimum price
    $285.00
    Suggested price
    $435.00

Author

About the Author

Contents

Table of Contents

Front Matter

  1. Preface
  2. Who This Book Is For
  3. How to Use This Book
  4. The Running Project: Pyre
  5. Toolchain Requirements
  6. Conventions Used in This Book

Chapter 1: The Bonfire — Why Rust, Why Now, Why From Python

  1. What We’re Building: Pyre
  2. The Parser
  3. The Allocation You Can’t See
  4. The Index
  5. The Query Engine
  6. Watching It Work (and Then Watching It Struggle)
  7. When Not to Reach for Rust
  8. What’s Coming

Chapter 2: Striking the Match — Rust for Pythonistas

  1. Variables: let Is Not Assignment
  2. Ownership: Reference Counting Made Explicit
  3. Ownership Is Not New
  4. Structs: Dataclasses With Teeth
  5. Enums: Python’s Enum + Pattern Matching + Algebraic Superpowers
  6. Traits: Protocols the Compiler Enforces
  7. Iterators: Generators That Are Zero-Cost
  8. The Module System: Cargo as Opinionated pyproject.toml
  9. Putting It Together: A Counter
  10. What We Didn’t Cover (Yet)
  11. What’s Coming

Chapter 3: First Sparks — PyO3 and Maturin

  1. The Tools: PyO3 and Maturin
  2. Setting Up the Crate
  3. The Rust Parser
  4. The Border Crossing Tax
  5. Building and Testing
  6. The Speedup
  7. Anatomy of a PyO3 Module
  8. The PyO3 Trinity: #[pyclass], #[pymethods], #[pyfunction]
  9. The Python<'py> Token
  10. Side Example: Wrapping the regex Crate
  11. What We Just Did

Chapter 4: Fanning the Flames — Lifetimes, Borrowing, and the Python Programmer’s Intuition

  1. The Newspaper Problem
  2. Lifetimes in Rust: The Syntax
  3. When Lifetimes Get Explicit
  4. The 'py Lifetime in PyO3
  5. Zero-Copy Parsing for Pyre
  6. Slices: Python’s memoryview, But Everywhere
  7. When to Clone and When to Borrow
  8. Side Example: Zero-Copy CSV Field Iterator
  9. What’s Coming

Chapter 5: Stoking the Coals — Error Handling and Testing Across the Boundary

  1. Errors as Values, Errors as Control Flow
  2. Building Pyre’s Error Hierarchy
  3. The ? Operator: Exception Propagation, Made Visible
  4. The Hidden Cost of Python Exceptions
  5. Testing Across the Boundary
  6. The “Test at the Boundary” Principle
  7. Side Example: A Validation Library
  8. What’s Coming

Act II: The Forge

  1. Chapters 6–10

Chapter 6: The Crucible — Data Structures That Cross Worlds

  1. The Lie of the List
  2. Building the Rust Index
  3. Implementing Python’s Dunder Methods
  4. The Search Method
  5. Side Example: A Trie With Dict-Like Ergonomics
  6. What’s Coming

Chapter 7: Parallel Universes — Concurrency Without the GIL

  1. The GIL Is Not Your Enemy (It’s Your Frenemy)
  2. py.allow_threads(): The Escape Hatch
  3. Rayon: Data Parallelism Made Easy
  4. Send and Sync: Compile-Time Thread Safety
  5. Pipeline Concurrency with Crossbeam
  6. What Happens When a Rayon Worker Panics?
  7. Threads vs. Processes vs. Async: When to Use What
  8. Side Example: Parallel File Hasher
  9. The Story So Far, In Numbers

Chapter 8: Async Tango — Rust Futures Meet Python Coroutines

  1. Two Event Loops Walk Into a Bar
  2. Bridging Tokio and Asyncio
  3. Backpressure and Flow Control
  4. Cancellation Doesn’t Cross the Border
  5. Fork Safety: The Runtime That Wasn’t
  6. What Happens When a Tokio Task Panics?
  7. Side Example: Async DNS Resolver
  8. The Borders of Two Worlds

Chapter 9: Unsafe at Any Speed — FFI, Raw Pointers, and When to Break the Rules

  1. The unsafe Keyword Is Honesty
  2. Memory-Mapped Files for Pyre
  3. Real CVEs That Rust Would Have Caught
  4. ctypes and cffi: Python-Side FFI
  5. Side Example: Three-Layer FFI
  6. What’s Coming

Chapter 10: The Shape of Things — Generics, Traits, and Protocol Design

  1. Generics vs. Duck Typing
  2. Monomorphization, or, Why Rust Generics Are Free
  3. Pyre’s Parseable Trait
  4. Static vs. Dynamic Dispatch
  5. Calling Python from Rust: The Trampoline
  6. Side Example: A Plugin System
  7. What We’ve Forged

Act III: Tempered Steel

  1. Chapters 11–15

Chapter 11: Ship It — Packaging, Distribution, and CI/CD

  1. The Wheel Was Invented Twice
  2. maturin Build vs. maturin Develop
  3. Wheel Tags and Platform Compatibility
  4. When Builds Go Wrong
  5. pyproject.toml Configuration
  6. Cross-Compilation
  7. GitHub Actions CI
  8. The Source Distribution Fallback
  9. Versioning Strategy
  10. Side Example: Minimal maturin Project From Scratch
  11. Shipping Is Believing

Chapter 12: Measure Twice — Profiling and Benchmarking Hybrid Systems

  1. The Benchmark That Lied
  2. Profiling the Python Side
  3. Profiling the Rust Side
  4. Benchmarking the Boundary
  5. Memory Profiling
  6. The Profile-Before-You-Optimize Discipline
  7. Side Example: The Full Optimization Loop
  8. pytest-benchmark
  9. The Numbers So Far

Chapter 13: Staying Alive — Observability, Logging, and Debugging Across the Boundary

  1. printf Debugging Is Underrated
  2. Connecting Rust’s tracing with Python’s logging
  3. Metrics: Counting What Matters
  4. Propagating Request Context
  5. When Rust Panics in Production
  6. Debugging Tips: Rust Inside Python
  7. Side Example: Unified Trace Decorator
  8. Staying Alive

Chapter 14: The Architecture of Hybrid Systems — Patterns and Anti-Patterns

  1. The Second System Effect, Bilingual Edition
  2. The Rewrite That Worked and the One That Didn’t
  3. The Thin Boundary Principle
  4. API Design at the Boundary
  5. Batch APIs: Amortizing the Border Tax
  6. Where to Put Validation
  7. Ownership at the Architecture Level
  8. When Two Languages Disagree
  9. The Two Architectures
  10. When to Use Subprocess Instead of In-Process FFI
  11. Side Example: Refactoring a Messy Boundary

Chapter 15: From Pyre to Fire — Where to Go From Here

  1. The Capstone: FastAPI + Rust Index
  2. Pyre’s Final Architecture
  3. The Ecosystem: Who Else Does This?
  4. The Future
  5. The Best Language Is Two Languages
  6. What You’ve Learned
  7. Starting Your Own Fire

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub