New advertisers: 50% off your first month with code FIRSTMONTHSee slots

Rust resource lifecycle guide covering RAII, lazy init, pooling, guards, and transaction scopes.

skills.sh installs
3.6K
GitHub stars
1.6K
last updated
Aug 21, 2026
View on GitHub

What it does

  • Covers the lifecycle decision flow: resource cost, scope, and error-time cleanup, with pattern-to-implementation mappings
  • Includes a pattern table (RAII, lazy init, pool, guard, scope) and templates for a Drop-based file guard and a OnceLock singleton
  • Lists common errors such as leaks, E0509 move-out-of-Drop fixes via Option::take(), and pool exhaustion from unreturned connections
  • Designed for Rust developers deciding when resources are created, shared, and cleaned up, and flags lazy_static! in favor of std::sync::OnceLock

Install

$ npx -y skills add actionbook/rust-skills --skill m12-lifecycle -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

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

PatternWhenImplementation
RAIIAuto cleanupDrop trait
Lazy initDeferred creationOnceLock, LazyLock
PoolReuse expensive resourcesr2d2, deadpool
GuardScoped accessMutexGuard pattern
ScopeTransaction boundaryCustom struct + Drop

Thinking Prompt

Before designing lifecycle:

  1. What's the resource cost?

    • Cheap → create per use
    • Expensive → pool or cache
    • Global → lazy singleton
  2. What's the scope?

    • Function-local → stack allocation
    • Request-scoped → passed or extracted
    • Application-wide → static or Arc
  3. 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):

Details

Repository
github.com/actionbook/rust-skills
Categories
Rust, Backend & APIs
First seen
May 16, 2026

More from actionbook/rust-skills

All 38

Similar to M12 Lifecycle

More Rust