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

Rust for C# Programmers: Complete Training Guide / 面向 C# 程序员的 Rust 完整培训指南

A comprehensive guide to learning Rust for developers with C# experience. This guide covers everything from basic syntax to advanced patterns, focusing on the conceptual shifts and practical differences between the two languages.

这是一本面向具有 C# 背景开发者的 Rust 学习指南,覆盖从基础语法到高级模式的完整内容,重点讲解两门语言在思维方式和实际用法上的差异。

Course Overview / 课程概览

  • The case for Rust - Why Rust matters for C# developers: performance, safety, and correctness / 为什么选择 Rust:Rust 为什么值得 C# 开发者学习,重点在性能、安全与正确性
  • Getting started - Installation, tooling, and your first program / 快速开始:安装、工具链与第一个程序
  • Basic building blocks - Types, variables, control flow / 基础构件:类型、变量、控制流
  • Data structures - Arrays, tuples, structs, collections / 数据结构:数组、元组、结构体、集合
  • Pattern matching and enums - Algebraic data types and exhaustive matching / 模式匹配与枚举:代数数据类型与穷尽匹配
  • Ownership and borrowing - Rust’s memory management model / 所有权与借用:Rust 的内存管理模型
  • Modules and crates - Code organization and dependencies / 模块与 crate:代码组织与依赖管理
  • Error handling - Result-based error propagation / 错误处理:基于 Result 的错误传播
  • Traits and generics - Rust’s type system / Trait 与泛型:Rust 类型系统
  • Closures and iterators - Functional programming patterns / 闭包与迭代器:函数式编程模式
  • Concurrency - Fearless concurrency with type-system guarantees, async/await deep dive / 并发:由类型系统保证的无畏并发,以及 async/await 深入解析
  • Unsafe Rust and FFI - When and how to go beyond safe Rust / Unsafe Rust 与 FFI:何时以及如何超越安全 Rust
  • Migration patterns - Real-world C# to Rust patterns and incremental adoption / 迁移模式:真实世界中的 C# 到 Rust 模式与渐进迁移
  • Best practices - Idiomatic Rust for C# developers / 最佳实践:适合 C# 开发者的 Rust 惯用法

Self-Study Guide / 自学指南

This material works both as an instructor-led course and for self-study. If you’re working through it on your own, here’s how to get the most out of it.

本材料既适合讲师授课,也适合自学。如果你打算自行学习,下面的建议能帮助你更高效地使用这套内容。

Pacing recommendations / 学习节奏建议:

Chapters / 章节Topic / 主题Suggested Time / 建议时间Checkpoint / 检查点
1-4Setup, types, control flow / 环境、类型、控制流1 day / 1 天You can write a CLI temperature converter in Rust / 你可以用 Rust 写一个命令行温度转换器
5-6Data structures, enums, pattern matching / 数据结构、枚举、模式匹配1-2 days / 1-2 天You can define an enum with data and match exhaustively on it / 你可以定义携带数据的枚举并用 match 进行穷尽匹配
7Ownership and borrowing / 所有权与借用1-2 days / 1-2 天You can explain why let s2 = s1 invalidates s1 / 你可以解释为什么 let s2 = s1 会使 s1 失效
8-9Modules, error handling / 模块、错误处理1 day / 1 天You can create a multi-file project that propagates errors with ? / 你可以创建一个多文件项目并用 ? 传播错误
10-12Traits, generics, closures, iterators / Trait、泛型、闭包、迭代器1-2 days / 1-2 天You can translate a LINQ chain to Rust iterators / 你可以把一段 LINQ 链改写成 Rust 迭代器
13Concurrency and async / 并发与异步1 day / 1 天You can write a thread-safe counter with Arc<Mutex<T>> / 你可以用 Arc<Mutex<T>> 写出线程安全计数器
14Unsafe Rust, FFI, testing / Unsafe Rust、FFI、测试1 day / 1 天You can call a Rust function from C# via P/Invoke / 你可以通过 P/Invoke 从 C# 调用 Rust 函数
15-16Migration, best practices, tooling / 迁移、最佳实践、工具链At your own pace / 自定节奏Reference material - consult as you write real code / 作为参考材料,在实际写代码时查阅
17Capstone project / 综合项目1-2 days / 1-2 天You have a working CLI tool that fetches weather data / 你拥有一个可用的命令行天气工具

How to use the exercises / 如何使用练习:

  • Chapters include hands-on exercises in collapsible <details> blocks with solutions / 各章包含可折叠 <details> 区块中的动手练习与答案
  • 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 / 如果卡住超过 15 分钟,就先看答案,理解后再关掉并从头重做
  • The Rust Playground lets you run code without a local install / Rust Playground 允许你在不本地安装的情况下运行代码

Difficulty indicators / 难度标记:

  • 🟢 Beginner - Direct translation from C# concepts / 初级:可以直接从 C# 概念迁移过来
  • 🟡 Intermediate - Requires understanding ownership or traits / 中级:需要理解所有权或 trait
  • 🔶 Advanced - Lifetimes, async internals, or unsafe code / 高级:生命周期、async 内部机制或 unsafe 代码

When you hit a wall / 遇到难点时:

  • Read the compiler error message carefully - Rust’s errors are exceptionally helpful / 仔细阅读编译器错误信息,Rust 的报错通常非常有帮助
  • 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 / Rust 标准库文档 非常优秀,遇到任何类型或方法都值得查
  • For deeper async patterns, see the companion Async Rust Training / 如果想深入学习异步模式,请参考配套的 Async Rust Training

Table of Contents / 目录

Part I - Foundations / 第一部分:基础

1. Introduction and Motivation / 1. 引言与动机 🟢

2. Getting Started / 2. 快速开始 🟢

3. Built-in Types and Variables / 3. 内建类型与变量 🟢

4. Control Flow / 4. 控制流 🟢

5. Data Structures and Collections / 5. 数据结构与集合 🟢

6. Enums and Pattern Matching / 6. 枚举与模式匹配 🟡

7. Ownership and Borrowing / 7. 所有权与借用 🟡

8. Crates and Modules / 8. Crate 与模块 🟢

9. Error Handling / 9. 错误处理 🟡

10. Traits and Generics / 10. Trait 与泛型 🟡

11. From and Into Traits / 11. FromInto Trait 🟡

12. Closures and Iterators / 12. 闭包与迭代器 🟡


Part II - Concurrency & Systems / 第二部分:并发与系统

13. Concurrency / 13. 并发 🔶

14. Unsafe Rust, FFI, and Testing / 14. Unsafe Rust、FFI 与测试 🟡


Part III - Migration & Best Practices / 第三部分:迁移与最佳实践

15. Migration Patterns and Case Studies / 15. 迁移模式与案例研究 🟡

16. Best Practices and Reference / 16. 最佳实践与参考 🟡


Capstone / 综合项目

17. Capstone Project / 17. 综合项目 🟡