
M12 Lifecycle
The m12-lifecycle skill guides developers through designing resource lifecycles in Rust by answering when resources should be created, used, and cleaned up. It covers patterns like RAII, lazy initialization, connection pooling, and scope guards, tracing decisions from domain constraints down to implementation details using ownership and the Drop trait. This skill is for Rust developers designing systems that manage expensive resources like database connections, helping them choose appropriate patterns based on resource cost, scope, and error handling requirements.
- skills.sh installs
- 764
- GitHub stars
- 1.3K
- last updated
- Jul 14, 2026
Install
$ npx -y skills add zhanghandong/rust-skills --skill m12-lifecycle --agent claude-codeInstalls into .claude/skills of the current project.
Files
Resource Lifecycle
Layer 2: Design Choices
Core Question
When should this resource be created, used, and cleaned up?
Before implementing lifecycle:
- What's the resource's scope?
- Who owns the cleanup responsibility?
- What happens on error?
Lifecycle Pattern → Implementation
| Pattern | When | Implementation |
|---|---|---|
| RAII | Auto cleanup | Drop trait |
| Lazy init | Deferred creation | OnceLock, LazyLock |
| Pool | Reuse expensive resources | r2d2, deadpool |
| Guard | Scoped access | MutexGuard pattern |
| Scope | Transaction boundary | Custom struct + Drop |
Thinking Prompt
Before designing lifecycle:
-
What's the resource cost?
- Cheap → create per use
- Expensive → pool or cache
- Global → lazy singleton
-
What's the scope?
- Function-local → stack allocation
- Request-scoped → passed or extracted
- Application-wide → static or Arc
-
What about errors?
- Cleanup must happen → Drop
- Cleanup is optional → explicit close
- Cleanup can fail → Result from close
Trace Up ↑
To domain constraints (Layer 3):
Sponsors
Details
- Repository
- github.com/zhanghandong/rust-skills
- Category
- Rust
More from zhanghandong/rust-skills
All 34- M14 Mental Model751
- M09 Domain750
- M11 Ecosystem747
- M13 Domain Error738
- Rust Trait Explorer731
- Rust Code Navigator728
- Unsafe Checker726
- Domain Cli709
- Rust Symbol Analyzer702
- Rust Call Graph699
- Rust Router698
- Rust Learner688
- Rust Deps Visualizer684
- Rust Skill Creator670
- Domain Web648
- Meta Cognition Parallel635
- Rust Daily635
- Domain Embedded625
- Domain Cloud Native596
- Domain Fintech581
- Domain Ml573
- Domain Iot568
- M15 Anti Pattern5.6K
- Coding Guidelines1.2K