← 返回日报
精读 预计 3 分钟

Introduction – Rust for Python Programmers

摘要

该教程专为有 Python 背景的开发者设计,通过概念类比(如 Cargo vs pip、Traits vs Duck Typing)降低学习门槛。内容涵盖基础语法、核心概念(所有权、生命周期)及进阶迁移(并发、PyO3 扩展)三大部分,并包含带答案的互动练习、难度分级说明以及一个完整的 CLI 项目实战,旨在帮助开发者完成从动态语言转向静态系统语言的思维转变。

荐读理由

针对 AI 工程中 Python 的性能瓶颈,你可以利用第 14 章的 PyO3 教程和第 15 章的迁移模式,将计算密集型模块重构为 Rust 扩展,在保留原有 Python 逻辑的同时解决 GIL 限制并提升系统稳定性。

原文

Rust for Python Programmers: Complete Training Guide

A comprehensive guide to learning Rust for developers with Python experience. This guide covers everything from basic syntax to advanced patterns, focusing on the conceptual shifts required when moving from a dynamically-typed, garbage-collected language to a statically-typed systems language with compile-time memory safety.

How to Use This Book

Self-study format: Work through Part I (ch 1–6) first — these map closely to Python concepts you already know. Part II (ch 7–12) introduces Rust-specific ideas like ownership and traits. Part III (ch 13–16) covers advanced topics and migration.

Pacing recommendations:

Chapters Topic Suggested Time Checkpoint
1–4 Setup, types, control flow 1 day You can write a CLI temperature converter in Rust
5–6 Data structures, enums, pattern matching 1–2 days You can define an enum with data and match exhaustively on it
7 Ownership and borrowing 1–2 days You can explain why let s2 = s1 invalidates s1
8–9 Modules, error handling 1 day You can create a multi-file project that propagates errors with ?
10–12 Traits, generics, closures, iterators 1–2 days You can translate a list comprehension to an iterator chain
13 Concurrency 1 day You can write a thread-safe counter with Arc<Mutex<T>>
14 Unsafe, PyO3, testing 1 day You can call a Rust function from Python via PyO3
15–16 Migration, best practices At your own pace Reference material — consult as you write real code
17 Capstone project 2–3 days Build a complete CLI app tying everything together

How to use the exercises:

  • Chapters include hands-on exercises in collapsible <details> blocks with solutions

  • Always try the exercise before expanding the solution. Struggling with the borrow checker is part of learning — the compiler’s error messages are your teacher

  • If you’re stuck for more than 15 minutes, expand the solution, study it, then close it and try again from scratch

  • The Rust Playground lets you run code without a local install

Difficulty indicators:

  • 🟢 Beginner — Direct translation from Python concepts

  • 🟡 Intermediate — Requires understanding ownership or traits

  • 🔴 Advanced — Lifetimes, async internals, or unsafe code

When you hit a wall:

  • Read the compiler error message carefully — Rust’s errors are exceptionally helpful

  • Re-read the relevant section; concepts like ownership (ch7) often click on the second pass

  • The Rust standard library docs are excellent — search for any type or method

  • For deeper async patterns, see the companion Async Rust Training


Table of Contents

Part I — Foundations

1. Introduction and Motivation 🟢

2. Getting Started 🟢

3. Built-in Types and Variables 🟢

4. Control Flow 🟢

5. Data Structures and Collections 🟢

6. Enums and Pattern Matching 🟡

Part II — Core Concepts

7. Ownership and Borrowing 🟡

8. Crates and Modules 🟢

9. Error Handling 🟡

10. Traits and Generics 🟡

11. From and Into Traits 🟡

12. Closures and Iterators 🟡

Part III — Advanced Topics & Migration

13. Concurrency 🔴

14. Unsafe Rust, FFI, and Testing 🔴

15. Migration Patterns 🟡

16. Best Practices 🟡


Part IV — Capstone

17. Capstone Project: CLI Task Manager 🔴


Hacker News · 70 赞 · 26 评 讨论 → 阅读原文 →

这条对你有帮助吗?