Rust 学习笔记 27:FFI 交互 (Foreign Function Interface)

Rust 学习笔记 27:FFI 交互 (Foreign Function Interface) “Rust is what C++ should have been.” (Debatable, but Rust interacts with C perfectly) FFI 允许 Rust 代码调用其他语言写的函数,或者让其他语言调用 Rust 函数。 由于 C 语言是计算机世界的"通用语" (Lingua Franca),FFI 通常指的就是与 C 语言的交互。 ...

2026-06-12 · 2 分钟 · 908 字 · 老墨

Rust 学习笔记 26:宏入门 (Macros)

Rust 学习笔记 26:宏入门 (Macros) “Code that writes code.” 我们从第一天开始就在用宏:println!。 宏 (Macro) 允许我们在编译期生成代码。这被称为元编程 (Metaprogramming)。 1. 宏 vs 函数 特性 宏 函数 参数数量 可变参数 (println!("{}, {}", a, b)) 固定参数个数 调用时机 编译期展开 (生成代码) 运行时调用 功能 可以定义 DSL,可以操作语法树 只能执行普通逻辑 维护性 编写复杂,调试困难 易于编写和调试 2. 声明式宏 (macro_rules!) 这是 Rust 中最常见的宏类型,类似于 match 匹配。 ...

2026-05-20 · 2 分钟 · 794 字 · 老墨

Rust 学习笔记 25:高级特性 (Advanced Features)

Rust 学习笔记 25:高级特性 (Advanced Features) “With great power comes great responsibility.” – Uncle Ben (actually unsafe block) Rust 通常强制执行内存安全规则,但有时我们需要绕过它们(比如操作硬件、与 C 交互)。这时,Unsafe Rust 就派上用场了。 此外,Rust 的 Trait 和类型系统还有很多高级用法,能让你的代码更具表现力。 ...

2026-04-15 · 3 分钟 · 1101 字 · 老墨

Rust 学习笔记 24:模式匹配详情 (Pattern Matching)

Rust 学习笔记 24:模式匹配详情 (Pattern Matching) “Patterns are the ultimate way to deconstruct reality.” 我们在前面的章节已经频繁使用了 match 和 let。其实,模式 (Pattern) 在 Rust 中无处不在。 只要涉及到数据赋值或参数传递,几乎都有模式的身影。 ...

2026-03-30 · 2 分钟 · 900 字 · 老墨

Rust 学习笔记 23:面向对象特性 (Object Oriented Features)

Rust 学习笔记 23:面向对象特性 (Object Oriented Features) “The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but you got a gorilla holding the banana and the entire jungle.” – Joe Armstrong Rust 是面向对象语言吗?这取决于你对 OOP 的定义。 如果定义是"封装、继承、多态",那么 Rust: ...

2026-01-20 · 3 分钟 · 1111 字 · 老墨

Rust 学习笔记 22:共享状态并发 (Shared-State Concurrency)

Rust 学习笔记 22:共享状态并发 (Shared-State Concurrency) “Do not communicate by sharing memory; instead, share memory by communicating.” – Go Language Slogan 虽然 Go 倡导通过通信来共享内存(Channel),但 Rust 同样提供了强大的共享状态并发工具。而且得益于所有权系统,Rust 中的锁是线程安全的(Thread Safety),你很难写出有数据竞争的代码。 ...

2025-11-30 · 2 分钟 · 962 字 · 老墨

Rust 学习笔记 21:并发编程 (无畏并发)

Rust 学习笔记 21:并发编程 (无畏并发) “Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.” – Rob Pike 在很多语言中,并发编程是噩梦。数据竞争 (Data Race)、死锁 (Deadlock)、竞态条件 (Race Condition) 让人防不胜防。 Rust 号称 “Fearless Concurrency”(无畏并发)。它利用所有权和类型系统,在编译期就把数据竞争扼杀在摇篮里。 ...

2025-11-29 · 2 分钟 · 816 字 · 老墨

Rust 学习笔记 20:项目实战二:构建 grep 命令行工具 (minigrep)

Rust 学习笔记 20:项目实战二:minigrep “UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” – Dennis Ritchie 到目前为止,我们已经学习了 Rust 的大部分核心特性。现在,让我们把它们串起来,复刻一个经典的命令行工具:grep。 我们的目标是创建一个 minigrep,它接受一个查询字符串和一个文件名,然后打印出文件中包含查询字符串的行。 ...

2025-11-04 · 3 分钟 · 1073 字 · 老墨

Rust 学习笔记 19:智能指针 (Smart Pointers)

Rust 学习笔记 19:智能指针 (Smart Pointers) “Pointer, I choose you!” 在 C++ 中,为了管理内存,我们有 unique_ptr 和 shared_ptr。 Rust 也有类似这一套,而且它是内存安全模型的重要补充。 普通引用 &T 和 &mut T 只是单纯的借用,不拥有数据。 而智能指针通常拥有它们指向的数据。 ...

2025-08-28 · 2 分钟 · 884 字 · 老墨

Rust 学习笔记 18:闭包与迭代器 (Closures & Iterators)

Rust 学习笔记 18:闭包与迭代器 (Closures & Iterators) “Functional programming is like backwards coding, everything is immutable and stateless… wait, Rust allows mutability?” Rust 不仅仅是系统编程语言,它还拥有强大的函数式编程特性。其中最核心的两个就是:闭包和迭代器。 ...

2025-08-20 · 2 分钟 · 926 字 · 老墨

Rust 学习笔记 17:自动化测试 (Automated Testing)

Rust 学习笔记 17:自动化测试 (Automated Testing) “Program testing can be a very effective way to show the presence of bugs, but it is hopelessly inadequate for showing their absence.” – Edsger W. Dijkstra 在 Go 中,我们创建 _test.go 文件,写 func TestXxx(t *testing.T)。 在 Rust 中,测试是一等公民。你不需要特殊的目录结构(单元测试),也不需要额外的测试框架。 ...

2025-06-10 · 2 分钟 · 925 字 · 老墨

Rust 学习笔记 15:Traits (特质)

Rust 学习笔记 15:Traits (特质) “If it walks like a duck and quacks like a duck, it must be a Trait.” 在 Go 语言中,接口 (Interface) 是隐式实现的。只要你的方法签名对上了,你就实现了接口。 在 Rust 中,Traits (特质) 必须要显式实现 (impl Trait for Type)。 ...

2025-05-20 · 2 分钟 · 934 字 · 老墨

Rust 学习笔记 16:生命周期 (Lifetimes)

Rust 学习笔记 16:生命周期 (Lifetimes) “To live is to suffer, to survive is to find some meaning in the suffering… of the borrow checker.” 生命周期 (Lifetimes) 是 Rust 中最令人困惑的概念,没有之一。 在 Go 或 Java 中,GC 会自动管理对象的死活。你不需要关心引用活多久,反正 GC 兜底。 在 C++ 中,你需要手动管理,稍有不慎就是 Use-After-Free。 ...

2025-05-20 · 3 分钟 · 1077 字 · 老墨

Rust 学习笔记 14:泛型 (Generics)

Rust 学习笔记 14:泛型 (Generics) “Abstraction without overhead.” 泛型是静态类型语言提高代码复用率的关键。 在 Go 中,我们习惯了 interface{} (或 any) 带来的运行时动态检查,或者复制粘贴代码。 Rust 的泛型则不同,它在编译期通过单态化 (Monomorphization) 展开代码,运行时没有性能损耗。 ...

2025-04-26 · 2 分钟 · 939 字 · 老墨

Rust 学习笔记 13:错误处理 (Error Handling)

Rust 学习笔记 13:错误处理 (Error Handling) “To err is human; to handle it safely is Rusty.” Go 开发者最熟悉的代码可能是 if err != nil。 Rust 也有类似的机制,但它利用强大的枚举类型 Result<T, E> 把错误处理提升到了一个新的高度。 ...

2025-03-11 · 3 分钟 · 1029 字 · 老墨

Rust 学习笔记 12:常用集合 (Collections)

Rust 学习笔记 12:常用集合 (Collections) “Data structures are the backbone of any program.” Rust 标准库提供了一系列非常实用的集合数据结构。和数组/元组不同,它们的数据存储在堆 (Heap) 上,这意味着长度可以动态变化。 ...

2025-02-18 · 3 分钟 · 1118 字 · 老墨

Rust 学习笔记 11:包与模块 (Packages & Modules)

Rust 学习笔记 11:包与模块 (Packages & Modules) “Organization is what you do before you do it, so that when you do it, it’s not all mixed up.” – A. A. Milne 欢迎来到 2025 年! 在完成了前 10 章的基础语法学习后,我们现在的 Rust 代码全都写在一个 main.rs 里。这对于写个猜数字游戏还行,但要写大项目,文件组织是必修课。 ...

2025-02-06 · 3 分钟 · 1072 字 · 老墨

Rust 学习笔记 10:项目实战一:猜数字

Rust 学习笔记 10:项目实战一:猜数字 “Programming is not about typing, it’s about thinking.” 之前的 9 篇笔记,我们一直在抠语法细节。 今天是圣诞节(假设),我们来点轻松的:用 Rust 写一个猜数字游戏。 ...

2024-12-25 · 2 分钟 · 915 字 · 老墨

Rust 学习笔记 09:枚举与模式匹配

Rust 学习笔记 09:枚举与模式匹配 “Null References: The Billion Dollar Mistake.” – Tony Hoare Go 语言没有枚举(只有 iota 常量),也没有代数数据类型,更没有模式匹配。所以这一章对于 Go 开发者来说,是全新的世界。 ...

2024-11-30 · 2 分钟 · 977 字 · 老墨

Rust 学习笔记 08:结构体 (Structs)

Rust 学习笔记 08:结构体 (Structs) “Structure is the maker of light.” – Louis Kahn 在 Go 中,我们用 struct 来组织数据,用 func (s *Struct) Method() 来定义方法。 在 Rust 中,这种感觉非常相似,但细节决定成败。 1. 定义与实例化 定义一个 User 结构体: ...

2024-10-15 · 3 分钟 · 1137 字 · 老墨

Rust 学习笔记 07:Slice 类型

Rust 学习笔记 07:Slice 类型 “Slice and dice your data safely.” 在 Go 语言中,Slice (切片) 是一个非常核心的概念,它底层是一个结构体 (ptr, len, cap)。 在 Rust 中,Slice 也是类似的,但它有一个本质的区别:Rust 的 Slice 是一种引用(Borrowed Type)。 ...

2024-09-08 · 3 分钟 · 1060 字 · 老墨

Rust 学习笔记 06:引用与借用 (References & Borrowing) 下

Rust 学习笔记 06:引用与借用 (References & Borrowing) 下 “Borrow checkers are the strict librarians of the programming world.” 上一集我们讲了所有权 (Ownership):把东西给别人(Move),自己就没了。这虽然安全,但太麻烦了。 ...

2024-08-25 · 3 分钟 · 1155 字 · 老墨

Rust 学习笔记 05:所有权 (Ownership) 上

Rust 学习笔记 05:所有权 (Ownership) 上 “I thought I knew what ownership meant until I met the borrow checker.” – Anonymous Rustacean 终于来到了 Rust 的核心 —— 所有权 (Ownership)。 作为 Go 开发者,我们习惯了 GC(垃圾回收)帮我们打理一切。我们随手创建一个指针,传给函数,传给 Channel,从来不需要关心它什么时候被释放。因为有 GC 在兜底。 ...

2024-07-20 · 3 分钟 · 1324 字 · 老墨

Rust 学习笔记 04:控制流程

Rust 学习笔记 04:控制流程 “Controlling complexity is the essence of computer programming.” – Brian Kernighan 写控制流程(条件判断、循环)是程序员每天都在做的事。 对于 Go 开发者来说,我们习惯了 if err != nil 和万能的 for 循环。 ...

2024-06-15 · 2 分钟 · 953 字 · 老墨

Rust 学习笔记 03:函数

Rust 学习笔记 03:函数 “Talk is cheap. Show me the code.” – Linus Torvalds 在这一课,我们来聊聊编程语言中最基础的构建块——函数。 如果你熟悉 Go,你可能会觉得:“函数嘛,不就是 func 换成了 fn,大括号里写逻辑吗?” ...

2024-05-18 · 3 分钟 · 1220 字 · 老墨

Rust 学习笔记 02:变量与可变性

Rust 学习笔记 02:变量与可变性 “Stability is not immobility.” – Prince Metternich 习惯了 Go 语言的 var 和 :=,第一次写 Rust 时,最让人抓狂的报错一定是 cannot assign twice to immutable variable。 在 Go 里,变量生来就是可变的(除了 const),但在 Rust 里,变量生来就是不可变的。 ...

2024-04-12 · 3 分钟 · 1487 字 · 老墨

Rust 学习笔记 01:简介与环境搭建

Rust 学习笔记 01:简介与环境搭建 “A language that doesn’t affect the way you think about programming, is not worth knowing.” – Alan Perlis 作为一名写了几年 Go 的程序员,习惯了 GC 的安逸,也忍受了 if err != nil 的繁琐。一直听说 Rust 有多强,但每次看到那陡峭的学习曲线和满屏的生命周期引用,都默默劝退。 ...

2024-03-05 · 2 分钟 · 966 字 · 老墨