Rust for the Python Programmer (logferry)
You already know Python. This guide teaches you Rust — starting from what you know, mapping every concept side by side, and ending with a real working project: a multi-threaded log ingestor that Python imports like any other library.
No prior Rust experience required. Each article shows the Python way first, then the idiomatic Rust equivalent, then explains what changed and why.
Running example:
logferry— a multi-threaded JSON log parser built with Rust and PyO3. Every chapter builds toward understanding how it works and how to extend it.
How to Read This Guide
| Your goal | Where to start |
|---|---|
| Completely new to Rust | Chapter 1 — Getting Started → read in order |
| Know Rust basics, want the Python bridge | Chapter 4 — PyO3 and logferry |
| Quick syntax lookup | Python → Rust Cheatsheet |
| Something broke | Troubleshooting |
Chapters
01 — Getting Started
Go from zero to a working Rust project in one sitting.
| Article | Description |
|---|---|
| What is Rust? | Why Python developers learn Rust, what the GIL costs you, and what Rust gives back |
| Installation | Install Rust on Windows, macOS, and Linux; verify your setup |
| Your First Project | cargo new, hello world, a JSON log counter, unit tests |
| IDE Setup | VS Code + rust-analyzer, RustRover, Neovim |
02 — Rust Language Fundamentals
A Python-first tour of the Rust language. Every article shows Python code first, then the Rust equivalent.
| Article | Python concept mapped | Rust concept learned |
|---|---|---|
| Variables and Types | x = 10, type hints |
let, mut, fixed-width integers, String vs &str |
| Functions and Control Flow | def, if/elif/else, for, match |
fn, implicit return, loop, ranges, exhaustive match |
| Ownership | GC / refcount | The one-owner rule, move semantics, Copy, clone() |
| Borrowing and Slices | Passing by reference | &T, &mut T, borrow rules, zero-copy slices |
| Structs and Enums | @dataclass, Enum |
struct, impl, enums with data, match destructuring |
| Error Handling | raise / try/except |
Option<T>, Result<T,E>, the ? operator |
| Collections | list, dict, set |
Vec, HashMap, HashSet, the entry API |
| Closures and Iterators | lambda, list comprehensions, map/filter |
Closures, move, lazy iterators, adapters |
| Traits and Generics | Protocol, ABC, TypeVar |
trait, Send + Sync, generics, lifetimes (intro) |
| Modules and Crates | import, packages |
mod, pub, use, crates, Cargo.toml |
03 — Project Setup and Tooling
cargo is pip + virtualenv + pytest + black in one tool. This chapter explains how.
| Article | Description |
|---|---|
| Cargo In Depth | Cargo.toml anatomy, build profiles, the full command reference |
| Dependencies | cargo add, crates.io, Python-to-Rust library analogue table |
| Testing | #[test], integration tests, and pytest for PyO3 modules |
| Daily Workflow | Inner loop, cargo watch, before-commit checklist, CI config |
04 — PyO3 and logferry
Write Rust, import it from Python. This chapter covers the full bridge — from a single function to a published wheel.
| Article | Description |
|---|---|
| PyO3 Overview | What PyO3 is, project setup, the type conversion table |
| Exposing Functions | #[pyfunction], type mapping, default arguments |
| Exposing Classes | #[pyclass], #[pymethods], read-only properties, __repr__ |
| Error Handling in PyO3 | PyResult, From<MyError> for automatic exception conversion |
| Multithreading and the GIL | py.allow_threads, thread::scope, why no Arc<Mutex<>> needed |
| logferry Walkthrough | Annotated end-to-end tour of src/lib.rs — every decision explained |
| Distribution | maturin build, cross-platform wheels, type stubs, mixed layouts |
05 — Reference
Quick-lookup tables for when you know what you want but need the exact syntax.
| Article | Description |
|---|---|
| Cargo Cheatsheet | Every cargo and maturin command in one place |
| Python → Rust Cheatsheet | Side-by-side syntax and concept lookup |
| Troubleshooting | Common compiler errors, PyO3 pitfalls, platform gotchas |
What is logferry?
logferry is the running example that ties this guide together. It is a multi-threaded JSON log ingestor exposed to Python via PyO3 — small enough to read in one sitting, but real enough to demonstrate every important Rust concept:
| Concept | Where it appears in logferry |
|---|---|
| Ownership across threads | thread::scope borrows lines into workers with no Arc |
Result-based error handling |
Parse errors counted and sampled; validation errors raised to Python |
| Traits as interfaces | Validator: Send + Sync — extensible, thread-safe, zero overhead |
| PyO3 type bridge | IngestStats fields auto-convert to Python int, dict, float|None |
See the project README for the quick-start API reference.