m01-ownership
所有権、借用、ライフタイムに関するエラー(値の移動、借用期間、所有権の問題など)を解決するために、コンパイラのエラーメッセージや関連キーワードから原因を特定し、適切な修正方法を提案するSkill。
📜 元の英語説明(参考)
CRITICAL: Use for ownership/borrow/lifetime issues. Triggers: E0382, E0597, E0506, E0507, E0515, E0716, E0106, value moved, borrowed value does not live long enough, cannot move out of, use of moved value, ownership, borrow, lifetime, 'a, 'static, move, clone, Copy, 所有权, 借用, 生命周期
🇯🇵 日本人クリエイター向け解説
所有権、借用、ライフタイムに関するエラー(値の移動、借用期間、所有権の問題など)を解決するために、コンパイラのエラーメッセージや関連キーワードから原因を特定し、適切な修正方法を提案するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o m01-ownership.zip https://jpskill.com/download/9260.zip && unzip -o m01-ownership.zip && rm m01-ownership.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/9260.zip -OutFile "$d\m01-ownership.zip"; Expand-Archive "$d\m01-ownership.zip" -DestinationPath $d -Force; ri "$d\m01-ownership.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
m01-ownership.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
m01-ownershipフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
所有権とライフタイム
レイヤー1: 言語の仕組み
中核となる問い
誰がこのデータを所有すべきか、そしてどのくらいの期間か?
所有権のエラーを修正する前に、データの役割を理解してください。
- 共有されるものか、排他的なものか?
- 短命か、長命か?
- 変換されるものか、単に読み取られるだけか?
エラー → 設計の問い
| エラー | 単に言うのではなく | 代わりに問う |
|---|---|---|
| E0382 | "Clone it" | 誰がこのデータを所有すべきか? |
| E0597 | "Extend lifetime" | スコープの境界は正しいか? |
| E0506 | "End borrow first" | ミューテーションは別の場所で行われるべきか? |
| E0507 | "Clone before move" | なぜ参照からムーブしているのか? |
| E0515 | "Return owned" | 呼び出し元がデータを所有すべきか? |
| E0716 | "Bind to variable" | なぜこれは一時的なのか? |
| E0106 | "Add 'a" | 実際のライフタイムの関係は何か? |
思考のプロンプト
所有権のエラーを修正する前に、以下を問いかけてください。
-
このデータのドメインにおける役割は何か?
- エンティティ(一意の識別子)→ 所有される
- 値オブジェクト(交換可能)→
clone/copyOK - 一時的なもの(計算結果)→ 再構築を検討
-
所有権の設計は意図的なものか?
- 意図的な設計 → 制約内で作業
- 偶発的なもの → 再設計を検討
-
症状を修正するか、再設計するか?
- 3回目の試行(Strike 3)の場合 → レイヤー2にエスカレート
上方向にトレース ↑
エラーが解消されない場合は、設計レイヤーまでトレースします。
E0382 (moved value)
↑ 問う: どのような設計上の選択がこの所有権パターンにつながったのか?
↑ 確認: 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> |
Clone-on-write | ミューテートされた場合のみアロケート | 変更する可能性がある |
エラーコードリファレンス
| エラー | 原因 | クイックフィックス |
|---|---|---|
| E0382 | 値がムーブされた | クローン、参照、または所有権の再設計 |
| E0597 | 参照がオーナーよりも長生き | オーナースコープを拡張するか、再構築 |
| E0506 | ボロー中に代入 | ミューテーションの前にボローを終了 |
| E0507 | ボローからムーブ | クローンまたは参照を使用 |
| E0515 | ローカル参照を返す | 所有された値を返す |
| E0716 | 一時的なものがドロップされた | 変数にバインド |
| E0106 | ライフタイムがない | 'a アノテーションを追加 |
アンチパターン
| アンチパターン | なぜ悪いのか | より良い方法 |
|---|---|---|
あらゆる場所で .clone() |
設計上の問題を隠蔽 | 所有権を適切に設計 |
| ボローチェッカーとの戦い | 複雑さを増大させる | コンパイラと連携 |
すべてに 'static |
柔軟性を制限 | 適切なライフタイムを使用 |
Box::leak でリーク |
メモリリーク | 適切なライフタイム設計 |
関連スキル
| いつ | 参照 |
|---|---|
| スマートポインタが必要 | m02-resource |
| 内部可変性が必要 | m03-mutability |
| データがドメインエンティティである | m09-domain |
| 所有権の概念を学習する | m14-mental-model |
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Ownership & Lifetimes
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? |
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
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? |
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)
Quick Reference
| 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> |
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 |
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 'a annotation |
Anti-Patterns
| 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 |
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 |