m01-ownership by zhanghandong/rust-skills
npx skills add https://github.com/zhanghandong/rust-skills --skill m01-ownership第一层:语言机制
谁应该拥有这些数据,以及拥有多久?
在修复所有权错误之前,先理解数据的作用:
| 错误 | 不要只说 | 而是应该问 |
|---|---|---|
| E0382 | "克隆它" | 谁应该拥有这些数据? |
| E0597 | "延长生命周期" | 作用域边界是否正确? |
| E0506 | "先结束借用" | 修改操作是否应该发生在别处? |
| E0507 | "移动前克隆" | 为什么我们要从引用中移动数据? |
| E0515 | "返回拥有所有权的值" | 调用者应该拥有数据吗? |
| E0716 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| "绑定到变量" |
| 为什么这是临时的? |
| E0106 | "添加 'a" | 实际的生命周期关系是什么? |
在修复所有权错误之前,先问:
当错误持续出现时,追溯到设计层:
E0382 (值已被移动)
↑ 问:是什么设计选择导致了这种所有权模式?
↑ 检查:m09-domain(这是实体还是值对象?)
↑ 检查:domain-*(应用了哪些约束?)
| 持续错误 | 追溯到 | 问题 |
|---|---|---|
| E0382 重复 | m02-resource | 是否应该使用 Arc/Rc 来共享? |
| E0597 重复 | m09-domain | 作用域边界位置是否正确? |
| E0506/E0507 | m03-mutability | 是否应该使用内部可变性? |
从设计决策到具体实现:
"数据需要被不可变地共享"
↓ 使用:Arc<T>(多线程)或 Rc<T>(单线程)
"数据需要独占所有权"
↓ 使用:移动语义,取得所有权
"数据是只读视图"
↓ 使用:&T(不可变借用)
| 模式 | 所有权 | 成本 | 使用场景 |
|---|---|---|---|
| 移动 | 转移 | 零 | 调用者不再需要数据 |
&T | 借用 | 零 | 需要只读访问 |
&mut T | 独占借用 | 零 | 需要修改 |
clone() | 复制 | 分配 + 复制 | 确实需要一个副本 |
Rc<T> | 共享(单线程) | 引用计数 | 单线程内共享 |
Arc<T> | 共享(多线程) | 原子引用计数 | 多线程间共享 |
Cow<T> | 写时克隆 | 若修改则分配 | 可能修改 |
| 错误 | 原因 | 快速修复 |
|---|---|---|
| E0382 | 值已被移动 | 克隆、使用引用或重新设计所有权 |
| E0597 | 引用比所有者存活更久 | 延长所有者作用域或重构 |
| E0506 | 在借用期间赋值 | 在修改前结束借用 |
| E0507 | 从借用中移出 | 克隆或使用引用 |
| E0515 | 返回局部引用 | 返回拥有所有权的值 |
| E0716 | 临时值被丢弃 | 绑定到变量 |
| E0106 | 缺少生命周期 | 添加 'a 标注 |
| 反模式 | 为何不好 | 更好的做法 |
|---|---|---|
到处使用 .clone() | 掩盖设计问题 | 妥善设计所有权 |
| 与借用检查器对抗 | 增加复杂性 | 与编译器协作 |
所有地方都用 'static | 限制灵活性 | 使用适当的生命周期 |
用 Box::leak 导致泄漏 | 内存泄漏 | 正确的生命周期设计 |
| 场景 | 参见 |
|---|---|
| 需要智能指针 | m02-resource |
| 需要内部可变性 | m03-mutability |
| 数据是领域实体 | m09-domain |
| 学习所有权概念 | m14-mental-model |
每周安装量
641
代码仓库
GitHub 星标数
924
首次出现
2026年1月20日
安全审计
安装于
opencode587
codex569
gemini-cli560
github-copilot546
amp495
kimi-cli491
Layer 1: Language Mechanics
Who should own this data, and for how long?
Before fixing ownership errors, understand the data's role:
| Error | Don't Just Say | Ask Instead |
|---|---|---|
| E0382 | "Clone it" | Who should own this data? |
| E0597 | "Extend lifetime" | Is the scope boundary correct? |
| E0506 | "End borrow first" | Should mutation happen elsewhere? |
| E0507 | "Clone before move" | Why are we moving from a reference? |
| E0515 | "Return owned" | Should caller own the data? |
| E0716 | "Bind to variable" | Why is this temporary? |
| E0106 | "Add 'a" | What is the actual lifetime relationship? |
Before fixing an ownership error, ask:
What is this data's domain role?
Is the ownership design intentional?
Fix symptom or redesign?
When errors persist, trace to design layer:
E0382 (moved value)
↑ Ask: What design choice led to this ownership pattern?
↑ Check: m09-domain (is this Entity or Value Object?)
↑ Check: domain-* (what constraints apply?)
| Persistent Error | Trace To | Question |
|---|---|---|
| E0382 repeated | m02-resource | Should use Arc/Rc for sharing? |
| E0597 repeated | m09-domain | Is scope boundary at right place? |
| E0506/E0507 | m03-mutability | Should use interior mutability? |
From design decisions to implementation:
"Data needs to be shared immutably"
↓ Use: Arc<T> (multi-thread) or Rc<T> (single-thread)
"Data needs exclusive ownership"
↓ Use: move semantics, take ownership
"Data is read-only view"
↓ Use: &T (immutable borrow)
| Pattern | Ownership | Cost | Use When |
|---|---|---|---|
| Move | Transfer | Zero | Caller doesn't need data |
&T | Borrow | Zero | Read-only access |
&mut T | Exclusive borrow | Zero | Need to modify |
clone() | Duplicate | Alloc + copy | Actually need a copy |
Rc<T> |
| Error | Cause | Quick Fix |
|---|---|---|
| E0382 | Value moved | Clone, reference, or redesign ownership |
| E0597 | Reference outlives owner | Extend owner scope or restructure |
| E0506 | Assign while borrowed | End borrow before mutation |
| E0507 | Move out of borrowed | Clone or use reference |
| E0515 | Return local reference | Return owned value |
| E0716 | Temporary dropped | Bind to variable |
| E0106 | Missing lifetime | Add 'a annotation |
| Anti-Pattern | Why Bad | Better |
|---|---|---|
.clone() everywhere | Hides design issues | Design ownership properly |
| Fight borrow checker | Increases complexity | Work with the compiler |
'static for everything | Restricts flexibility | Use appropriate lifetimes |
Leak with Box::leak | Memory leak | Proper lifetime design |
| When | See |
|---|---|
| Need smart pointers | m02-resource |
| Need interior mutability | m03-mutability |
| Data is domain entity | m09-domain |
| Learning ownership concepts | m14-mental-model |
Weekly Installs
641
Repository
GitHub Stars
924
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode587
codex569
gemini-cli560
github-copilot546
amp495
kimi-cli491
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
103,800 周安装
智能体技能查找与安装指南 - 使用Skills CLI快速扩展AI助手能力
599 周安装
MarkItDown - 微软开源文件转Markdown工具,支持PDF/Word/PPT/图像OCR/音频转录
600 周安装
Stable Diffusion图像生成指南:使用HuggingFace Diffusers库进行AI绘图与图像转换
601 周安装
Claude Code技能改进工具:自动化技能质量审查与修复方法指南
601 周安装
Claude技能创建器 - 官方技能开发工具,AI技能制作与迭代指南
602 周安装
OpenAI图像生成技能:AI绘图、图片编辑与批量生成,支持网站素材、UI设计、产品模型
602 周安装
| Shared (single) |
| Ref count |
| Single-thread sharing |
Arc<T> | Shared (multi) | Atomic ref count | Multi-thread sharing |
Cow<T> | Clone-on-write | Alloc if mutated | Might modify |