m14-mental-model by zhanghandong/rust-skills
npx skills add https://github.com/zhanghandong/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 |
每周安装量
589
仓库
GitHub 星标数
912
首次出现
Jan 20, 2026
安全审计
安装于
opencode543
codex528
gemini-cli517
github-copilot504
amp461
kimi-cli456
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
589
Repository
GitHub Stars
912
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode543
codex528
gemini-cli517
github-copilot504
amp461
kimi-cli456
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
103,800 周安装
OpenAPI 转 TypeScript 工具 - 自动生成 API 接口与类型守卫
563 周安装
数据库模式设计器 - 内置最佳实践,自动生成生产级SQL/NoSQL数据库架构
564 周安装
Rust Unsafe代码检查器 - 安全使用Unsafe Rust的完整指南与最佳实践
564 周安装
.NET并发编程模式指南:async/await、Channels、Akka.NET选择决策树
565 周安装
韩语语法检查器 - 基于国立国语院标准的拼写、空格、语法、标点错误检测与纠正
565 周安装
技能安全扫描器 - 检测Claude技能安全漏洞,防范提示注入与恶意代码
565 周安装