m01-ownership
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOwnership & Lifetimes
Ownership & Lifetimes
Layer 1: Language Mechanics
Layer 1: Language Mechanics
Core Question
核心问题
Who should own this data, and for how long?
Before fixing ownership errors, understand the data's role:
- Is it shared or exclusive?
- Is it short-lived or long-lived?
- Is it transformed or just read?
谁应该拥有这份数据?拥有时长是多久?
在修复所有权相关错误之前,请先明确数据的角色:
- 它是共享的还是独占的?
- 它是短生命周期还是长生命周期?
- 它会被转换还是仅用于读取?
Error → Design Question
错误 → 设计问题
| 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? |
| 错误码 | 不要只说 | 应转而提问 |
|---|---|---|
| E0382 | “克隆它” | 谁应该拥有这份数据? |
| E0597 | “延长生命周期” | 作用域边界是否合理? |
| E0506 | “先结束借用” | 变更操作是否应该在其他位置进行? |
| E0507 | “转移前先克隆” | 为什么要从引用中转移值? |
| E0515 | “返回拥有权” | 调用者是否应该拥有这份数据? |
| E0716 | “绑定到变量” | 为什么这是临时值? |
| E0106 | “添加'a” | 实际的生命周期关系是什么? |
Thinking Prompt
思考提示
Before fixing an ownership error, ask:
-
What is this data's domain role?
- Entity (unique identity) → owned
- Value Object (interchangeable) → clone/copy OK
- Temporary (computation result) → maybe restructure
-
Is the ownership design intentional?
- By design → work within constraints
- Accidental → consider redesign
-
Fix symptom or redesign?
- If Strike 3 (3rd attempt) → escalate to Layer 2
在修复所有权错误之前,请先思考:
-
这份数据在业务领域中的角色是什么?
- 实体(唯一标识)→ 拥有所有权
- 值对象(可互换)→ 允许克隆/复制
- 临时值(计算结果)→ 可能需要重构代码
-
当前的所有权设计是有意为之吗?
- 有意设计 → 在约束范围内解决问题
- 意外产生 → 考虑重新设计
-
修复表面问题还是重新设计?
- 如果是第三次尝试仍未解决 → 升级到Layer 2层面分析
Trace Up ↑
向上追溯 ↑
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? |
当错误持续存在时,追溯到设计层面:
E0382 (moved value)
↑ 提问:是什么设计选择导致了这种所有权模式?
↑ 检查:m09-domain(这是实体还是值对象?)
↑ 检查:domain-*(有哪些约束条件?)| 持续出现的错误 | 追溯至 | 问题 |
|---|---|---|
| E0382重复出现 | m02-resource | 是否应该使用Arc/Rc进行共享? |
| E0597重复出现 | m09-domain | 作用域边界是否设置在正确位置? |
| E0506/E0507 | m03-mutability | 是否应该使用内部可变性? |
Trace Down ↓
向下落地 ↓
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)从设计决策到具体实现:
“数据需要被不可变共享”
↓ 使用:Arc<T>(多线程)或Rc<T>(单线程)
“数据需要独占所有权”
↓ 使用:move语义,获取所有权
“数据是只读视图”
↓ 使用:&T(不可变借用)Quick Reference
快速参考
| Pattern | Ownership | Cost | Use When |
|---|---|---|---|
| Move | Transfer | Zero | Caller doesn't need data |
| Borrow | Zero | Read-only access |
| Exclusive borrow | Zero | Need to modify |
| Duplicate | Alloc + copy | Actually need a copy |
| Shared (single) | Ref count | Single-thread sharing |
| Shared (multi) | Atomic ref count | Multi-thread sharing |
| Clone-on-write | Alloc if mutated | Might modify |
| 模式 | 所有权特性 | 成本 | 使用场景 |
|---|---|---|---|
| Move | 转移所有权 | 零成本 | 调用者不再需要该数据 |
| 借用 | 零成本 | 只读访问 |
| 独占借用 | 零成本 | 需要修改数据 |
| 复制数据 | 内存分配+复制 | 确实需要一份副本 |
| 共享所有权(单线程) | 引用计数 | 单线程环境下共享数据 |
| 共享所有权(多线程) | 原子引用计数 | 多线程环境下共享数据 |
| 写时复制 | 仅在修改时分配内存 | 可能会修改数据 |
Error Code Reference
错误码参考
| 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 |
| 错误码 | 原因 | 快速修复方案 |
|---|---|---|
| E0382 | 值已转移 | 克隆、引用或重新设计所有权 |
| E0597 | 引用的生命周期超过所有者 | 延长所有者作用域或重构代码 |
| E0506 | 借用期间进行赋值 | 先结束借用再执行变更操作 |
| E0507 | 从借用中转移值 | 克隆或使用引用 |
| E0515 | 返回局部变量的引用 | 返回拥有所有权的值 |
| E0716 | 临时值已被销毁 | 绑定到变量 |
| E0106 | 缺少生命周期标注 | 添加 |
Anti-Patterns
反模式
| Anti-Pattern | Why Bad | Better |
|---|---|---|
| Hides design issues | Design ownership properly |
| Fight borrow checker | Increases complexity | Work with the compiler |
| Restricts flexibility | Use appropriate lifetimes |
Leak with | Memory leak | Proper lifetime design |
| 反模式 | 问题所在 | 更佳方案 |
|---|---|---|
到处使用 | 隐藏设计问题 | 合理设计所有权 |
| 对抗借用检查器 | 增加复杂度 | 配合编译器解决问题 |
所有场景都用 | 限制灵活性 | 使用合适的生命周期 |
用 | 内存泄漏 | 合理设计生命周期 |
Related Skills
相关技能
| When | See |
|---|---|
| Need smart pointers | m02-resource |
| Need interior mutability | m03-mutability |
| Data is domain entity | m09-domain |
| Learning ownership concepts | m14-mental-model |
| 场景 | 参考内容 |
|---|---|
| 需要使用智能指针 | m02-resource |
| 需要使用内部可变性 | m03-mutability |
| 数据是领域实体 | m09-domain |
| 学习所有权概念 | m14-mental-model |