m14-mental-model by actionbook/rust-skills
npx skills add https://github.com/actionbook/rust-skills --skill m14-mental-model第二层:设计选择
思考这个 Rust 概念的正确方式是什么?
在学习或解释 Rust 时:
| 概念 | 心智模型 | 类比 |
|---|---|---|
| 所有权 | 唯一钥匙 | 只有一个人有房门钥匙 |
| 移动 | 钥匙移交 | 把你的钥匙给别人 |
&T | 借阅以供读取 | 借出一本书 |
&mut T | 独占编辑 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 只有你可以编辑文档 |
生命周期 'a | 有效作用域 | "票据有效期至..." |
Box<T> | 堆指针 | 电视的遥控器 |
Rc<T> | 共享所有权 | 多个遥控器,最后一个关闭 |
Arc<T> | 线程安全的 Rc | 来自任何房间的遥控器 |
| 来源 | 关键转变 |
|---|---|
| Java/C# | 默认拥有值,而非引用 |
| C/C++ | 编译器强制执行安全规则 |
| Python/Go | 无 GC,确定性析构 |
| 函数式 | 通过所有权实现安全的可变性 |
| JavaScript | 无 null,改用 Option |
当对 Rust 感到困惑时:
所有权模型是什么?
Rust 提供了什么保证?
编译器在告诉我什么?
通往设计理解(第二层):
"为什么我在 Rust 中不能做 X?"
↑ 提问:会违反什么安全保证?
↑ 检查:m01-m07 中正在执行的规则
↑ 提问:预期的设计模式是什么?
通往实现(第一层):
"我理解了概念,现在如何实现?"
↓ m01-ownership:所有权模式
↓ m02-resource:智能指针选择
↓ m07-concurrency:线程安全
| 错误 | 错误模型 | 正确模型 |
|---|---|---|
| E0382 移动后使用 | GC 会清理 | 所有权 = 唯一钥匙转移 |
| E0502 借用冲突 | 多个写入者可以 | 一次只能有一个写入者 |
| E0499 多个可变借用 | 别名化可变 | 可变操作需要独占访问 |
| E0106 缺少生命周期 | 忽略作用域 | 引用具有有效作用域 |
E0507 无法从 &T 移动 | 隐式克隆 | 引用不拥有数据 |
| 已弃用 | 更好 |
|---|---|
| "Rust 像 C++" | 不同的所有权模型 |
| "生命周期是 GC" | 编译时有效作用域 |
| "克隆解决一切" | 重构所有权 |
| "对抗借用检查器" | 与编译器协作 |
"使用 unsafe 规避规则" | 首先理解安全模式 |
Stack Heap
+----------------+ +----------------+
| main() | | |
| s1 ─────────────────────> │ "hello" |
| | | |
| fn takes(s) { | | |
| s2 (moved) ─────────────> │ "hello" |
| } | | (s1 invalid) |
+----------------+ +----------------+
移动后:s1 不再有效
+----------------+
| data: String |────────────> "hello"
+----------------+
↑
│ &data (不可变借用)
│
+------+------+
| reader1 reader2 (多个可以)
+------+------+
+----------------+
| data: String |────────────> "hello"
+----------------+
↑
│ &mut data (可变借用)
│
+------+
| writer (仅一个)
+------+
| 阶段 | 重点 | 技能 |
|---|---|---|
| 初学者 | 所有权基础 | m01-ownership, m14-mental-model |
| 中级 | 智能指针,错误处理 | m02, m06 |
| 高级 | 并发,unsafe | m07, unsafe-checker |
| 专家 | 设计模式 | m09-m15, domain-* |
| 何时 | 查看 |
|---|---|
| 所有权错误 | m01-ownership |
| 智能指针 | m02-resource |
| 并发 | m07-concurrency |
| 反模式 | m15-anti-pattern |
每周安装数
38
仓库
GitHub 星标数
828
首次出现
2026年1月23日
安全审计
安装于
opencode35
gemini-cli35
codex34
github-copilot31
claude-code30
cursor29
Layer 2: Design Choices
What's the right way to think about this Rust concept?
When learning or explaining Rust:
| Concept | Mental Model | Analogy |
|---|---|---|
| Ownership | Unique key | Only one person has the house key |
| Move | Key handover | Giving away your key |
&T | Lending for reading | Lending a book |
&mut T | Exclusive editing | Only you can edit the doc |
Lifetime 'a | Valid scope | "Ticket valid until..." |
Box<T> | Heap pointer | Remote control to TV |
Rc<T> | Shared ownership | Multiple remotes, last turns off |
Arc<T> | Thread-safe Rc | Remotes from any room |
| From | Key Shift |
|---|---|
| Java/C# | Values are owned, not references by default |
| C/C++ | Compiler enforces safety rules |
| Python/Go | No GC, deterministic destruction |
| Functional | Mutability is safe via ownership |
| JavaScript | No null, use Option instead |
When confused about Rust:
What's the ownership model?
What guarantee is Rust providing?
What's the compiler telling me?
To design understanding (Layer 2):
"Why can't I do X in Rust?"
↑ Ask: What safety guarantee would be violated?
↑ Check: m01-m07 for the rule being enforced
↑ Ask: What's the intended design pattern?
To implementation (Layer 1):
"I understand the concept, now how do I implement?"
↓ m01-ownership: Ownership patterns
↓ m02-resource: Smart pointer choice
↓ m07-concurrency: Thread safety
| Error | Wrong Model | Correct Model |
|---|---|---|
| E0382 use after move | GC cleans up | Ownership = unique key transfer |
| E0502 borrow conflict | Multiple writers OK | Only one writer at a time |
| E0499 multiple mut borrows | Aliased mutation | Exclusive access for mutation |
| E0106 missing lifetime | Ignoring scope | References have validity scope |
E0507 cannot move from &T | Implicit clone | References don't own data |
| Deprecated | Better |
|---|---|
| "Rust is like C++" | Different ownership model |
| "Lifetimes are GC" | Compile-time validity scope |
| "Clone solves everything" | Restructure ownership |
| "Fight the borrow checker" | Work with the compiler |
"unsafe to avoid rules" | Understand safe patterns first |
Stack Heap
+----------------+ +----------------+
| main() | | |
| s1 ─────────────────────> │ "hello" |
| | | |
| fn takes(s) { | | |
| s2 (moved) ─────────────> │ "hello" |
| } | | (s1 invalid) |
+----------------+ +----------------+
After move: s1 is no longer valid
+----------------+
| data: String |────────────> "hello"
+----------------+
↑
│ &data (immutable borrow)
│
+------+------+
| reader1 reader2 (multiple OK)
+------+------+
+----------------+
| data: String |────────────> "hello"
+----------------+
↑
│ &mut data (mutable borrow)
│
+------+
| writer (only one)
+------+
| Stage | Focus | Skills |
|---|---|---|
| Beginner | Ownership basics | m01-ownership, m14-mental-model |
| Intermediate | Smart pointers, error handling | m02, m06 |
| Advanced | Concurrency, unsafe | m07, unsafe-checker |
| Expert | Design patterns | m09-m15, domain-* |
| When | See |
|---|---|
| Ownership errors | m01-ownership |
| Smart pointers | m02-resource |
| Concurrency | m07-concurrency |
| Anti-patterns | m15-anti-pattern |
Weekly Installs
38
Repository
GitHub Stars
828
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode35
gemini-cli35
codex34
github-copilot31
claude-code30
cursor29
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
120,000 周安装
Azure 可观测性服务指南:Monitor、App Insights、Log Analytics 监控与 KQL 查询
102,200 周安装
Microsoft Entra应用注册指南:Azure AD应用配置、API权限与OAuth流程详解
103,100 周安装
Azure 验证工具 - Microsoft GitHub Copilot for Azure 部署前检查指南
103,100 周安装
Azure资源可视化工具 - 自动生成架构图,分析资源依赖关系
103,100 周安装
Azure 资源查找工具:跨订阅查询、发现孤立资源、审计标签配置
103,100 周安装
Azure RBAC 权限管理工具 - Microsoft GitHub Copilot for Azure 最小角色分配指南
103,100 周安装