Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Installation and Setup
安装与环境准备

What you’ll learn: How to install Rust and its toolchain, how Cargo compares with pip and Poetry, how to set up an IDE, how the first Hello, world! program differs from Python, and which core Rust keywords map to familiar Python ideas.
本章将学习: 如何安装 Rust 及其工具链,Cargo 和 pip、Poetry 的对应关系,如何配置 IDE,第一段 Hello, world! 程序和 Python 有什么差异,以及哪些 Rust 关键字可以映射到熟悉的 Python 概念。

Difficulty: 🟢 Beginner
难度: 🟢 入门

Installing Rust
安装 Rust

# Install Rust via rustup (Linux/macOS/WSL)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Verify installation
rustc --version
cargo --version

# Update Rust
rustup update

Rust installation is intentionally simple: rustup manages the compiler, standard library, Cargo, and toolchain updates in one place.
Rust 的安装流程相对统一,rustup 会同时管理编译器、标准库、Cargo 和后续工具链更新,省掉了不少来回折腾环境的麻烦。

Rust Tools vs Python Tools
Rust 工具链与 Python 工具链对照

Purpose
用途
PythonRust
Language runtime
语言运行核心
python interpreterrustc compiler
Package manager
包管理
pip / poetry / uvcargo
Project config
项目配置
pyproject.tomlCargo.toml
Lock file
锁文件
poetry.lock / requirements.txtCargo.lock
Virtual env
虚拟环境
venv / condaNot needed
通常不需要
Formatter
格式化
black / ruff formatrustfmt / cargo fmt
Linter
静态检查
ruff / flake8 / pylintclippy / cargo clippy
Type checker
类型检查
mypy / pyrightBuilt into compiler
编译器内建
Test runner
测试
pytestcargo test
Docs
文档
sphinx / mkdocscargo doc
REPL
交互式环境
python / ipythonNone
没有内建 REPL

IDE Setup
IDE 配置

VS Code (recommended):
VS Code(推荐入门使用):

Extensions to install:
- rust-analyzer
- Even Better TOML
- CodeLLDB

# Python equivalent mapping:
# rust-analyzer ≈ Pylance
# cargo clippy  ≈ ruff
建议安装的扩展:
- `rust-analyzer`:核心中的核心,负责补全、跳转、类型信息等 IDE 能力
- `Even Better TOML`:让 `Cargo.toml` 这类文件的编辑体验正常起来
- `CodeLLDB`:调试支持

如果非要类比:
- `rust-analyzer` 的定位有点像 Python 世界里的 Pylance
- `cargo clippy` 的角色有点像 `ruff`,但它还会兼顾更多正确性检查

Your First Rust Program
第一段 Rust 程序

Python Hello World
Python 版 Hello World

# hello.py — just run it
print("Hello, World!")

# Run:
# python hello.py

Rust Hello World
Rust 版 Hello World

// src/main.rs — must be compiled first
fn main() {
    println!("Hello, World!");   // println! is a macro
}

// Build and run:
// cargo run

Key Differences for Python Developers
Python 开发者最先要适应的差别

Python:                              Rust:
─────────                            ─────
- No main() needed                   - fn main() is the entry point
- Indentation = blocks               - Curly braces {} = blocks
- print() is a function              - println!() is a macro
- No semicolons                      - Semicolons end statements
- No type declarations               - Types inferred but always known
- Interpreted (run directly)         - Compiled first
- Errors at runtime                  - Most errors at compile time
最直观的区别有这几条:
- Python 不要求 `main()`,Rust 由 `fn main()` 作为入口
- Python 用缩进表示代码块,Rust 用花括号
- `print()` 在 Python 里是函数,`println!()` 在 Rust 里是宏,后面的 `!` 很关键
- Python 基本看不到分号,Rust 里分号会结束语句
- Rust 常常能做类型推导,但类型始终是明确存在的
- Python 通常边解释边运行,Rust 先编译再执行
- 很多 Python 运行时问题,会被 Rust 提前到编译期

Creating Your First Project
创建第一个项目

# Python                              # Rust
mkdir myproject                        cargo new myproject
cd myproject                           cd myproject
python -m venv .venv                   # No virtual env needed
source .venv/bin/activate              # No activation needed
# Create files manually               # src/main.rs already created
# Python project structure:            Rust project structure:
# myproject/                           myproject/
# ├── pyproject.toml                   ├── Cargo.toml
# ├── src/                             ├── src/
# │   └── myproject/                   │   └── main.rs
# │       ├── __init__.py              └── (no __init__.py needed)
# │       └── main.py
# └── tests/
#     └── test_main.py
graph TD
    subgraph Python ["Python Project<br/>Python 项目"]
        PP["pyproject.toml"] --- PS["src/"]
        PS --- PM["myproject/"]
        PM --- PI["__init__.py"]
        PM --- PMN["main.py"]
        PP --- PT["tests/"]
    end
    subgraph Rust ["Rust Project<br/>Rust 项目"]
        RC["Cargo.toml"] --- RS["src/"]
        RS --- RM["main.rs"]
        RC --- RTG["target/<br/>自动生成"]
    end
    style Python fill:#ffeeba
    style Rust fill:#d4edda

Key difference: Rust project layout is usually simpler. There is no __init__.py, no virtual environment activation step, and no split between several competing packaging tools.
关键差异: Rust 项目结构通常更简单,没有 __init__.py,没有虚拟环境激活这一步,也没有多套打包工具并存带来的混乱。


Cargo vs pip/Poetry
Cargo 与 pip / Poetry 的对应关系

Project Configuration
项目配置

# Python — pyproject.toml
[project]
name = "myproject"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
    "requests>=2.28",
    "pydantic>=2.0",
]

[project.optional-dependencies]
dev = ["pytest", "ruff", "mypy"]
# Rust — Cargo.toml
[package]
name = "myproject"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = "0.12"
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
# Test dependencies — only compiled for `cargo test`

Common Cargo Commands
常用 Cargo 命令

# Python equivalent                # Rust
pip install requests               cargo add reqwest
pip install -r requirements.txt    cargo build
pip install -e .                   cargo build
python -m pytest                   cargo test
python -m mypy .                   # Built into compiler
ruff check .                       cargo clippy
ruff format .                      cargo fmt
python main.py                     cargo run
python -c "..."                    # No equivalent

# Rust-specific:
cargo new myproject
cargo build --release
cargo doc --open
cargo update

Cargo combines package management, dependency resolution, building, testing, formatting entry points, and documentation into one consistent interface. That unified experience is one of Rust’s strongest ergonomic advantages.
Cargo 把包管理、依赖解析、构建、测试、格式化入口和文档生成统一到了一个接口里。这种一致性,本身就是 Rust 工具链很大的优势。


Essential Rust Keywords for Python Developers
Python 开发者需要先认识的 Rust 关键字

Variable and Mutability Keywords
变量与可变性

#![allow(unused)]
fn main() {
let name = "Alice";
// name = "Bob";             // ❌ immutable by default

let mut count = 0;
count += 1;

const MAX_SIZE: usize = 1024;

static VERSION: &str = "1.0";
}

let declares a variable, but it is immutable by default. mut is an explicit opt-in to changeability. That single design choice already separates Rust sharply from Python’s default-all-mutable feel.
let 用来声明变量,但默认不可变。mut 则是显式打开可变性。这一个设计决定,就已经让 Rust 和 Python “默认都能改”的感觉拉开了很大距离。

Ownership and Borrowing Keywords
所有权与借用

#![allow(unused)]
fn main() {
fn print_name(name: &str) { }

fn append(list: &mut Vec<i32>) { }

let s1 = String::from("hello");
let s2 = s1;
// println!("{}", s1);  // ❌ value moved
}

These concepts have no direct Python equivalent. & means borrowing, &mut means mutable borrowing, and assignment may move ownership rather than copy a reference.
这几样东西在 Python 里都没有完全对应物。& 表示借用,&mut 表示可变借用,而赋值在 Rust 里可能发生的是所有权转移,不只是“多了一个引用名”。

Type Definition Keywords
类型定义相关关键字

#![allow(unused)]
fn main() {
struct Point {
    x: f64,
    y: f64,
}

enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

impl Point {
    fn distance(&self) -> f64 {
        (self.x.powi(2) + self.y.powi(2)).sqrt()
    }
}

trait Drawable {
    fn draw(&self);
}

type UserId = i64;
}

Control Flow Keywords
控制流关键字

#![allow(unused)]
fn main() {
match value {
    1 => println!("one"),
    2 | 3 => println!("two or three"),
    _ => println!("other"),
}

if let Some(x) = optional_value {
    println!("{}", x);
}

loop {
    break;
}

for item in collection.iter() {
    println!("{}", item);
}

while let Some(item) = stack.pop() {
    process(item);
}
}

match and if let deserve special attention. They make destructuring and branch selection much more central than in everyday Python code.
matchif let 尤其值得重点适应。它们让模式匹配和结构拆解在日常代码里出现得远比 Python 更频繁。

Visibility Keywords
可见性关键字

#![allow(unused)]
fn main() {
pub fn greet() { }

pub(crate) fn internal() { }

fn private_helper() { }
}

Python’s privacy is mostly by convention. Rust’s privacy is enforced by the compiler, and that tends to make module boundaries far more explicit.
Python 的“私有”大多靠约定,Rust 的可见性则由编译器强制执行,因此模块边界通常会清楚得多。


Exercises
练习

🏋️ Exercise: First Rust Program
🏋️ 练习:第一段 Rust 程序

Challenge: Create a new Rust project and write a program that declares a name, counts from 1 to 5, prints greeting messages, and then reports whether the final count is even or odd with match.
挑战:新建一个 Rust 项目,声明一个名字变量,用循环把计数加到 5,每次打印问候语,最后再用 match 判断计数结果是奇数还是偶数。

🔑 Solution
🔑 参考答案
cargo new hello_rust && cd hello_rust
fn main() {
    let name = "Pythonista";
    let mut count = 0u32;

    for _ in 1..=5 {
        count += 1;
        println!("Hello, {name}! (count: {count})");
    }

    let parity = match count % 2 {
        0 => "even",
        _ => "odd",
    };
    println!("Final count {count} is {parity}");
}

Key takeaways:
核心收获:

  • let is immutable by default.
    let 默认不可变。
  • 1..=5 is an inclusive range, similar to Python’s range(1, 6).
    1..=5 是包含末尾的区间,类似 Python 的 range(1, 6)
  • match is an expression and can return a value.
    match 是表达式,可以直接产出值。
  • There is no if __name__ == "__main__" ceremony; fn main() is enough.
    这里没有 if __name__ == "__main__" 这层样板,写 fn main() 就够了。