🎯

m12-lifecycle

🎯Skill

from goooice/rust-skills

VibeIndex|
What it does

Designs resource lifecycles by determining optimal creation, usage, and cleanup strategies for efficient and safe resource management across different scopes and scenarios.

πŸ“¦

Part of

goooice/rust-skills(35 items)

m12-lifecycle

Installation

Add MarketplaceAdd marketplace to Claude Code
/plugin marketplace add ZhangHanDong/rust-skills
Install PluginInstall plugin from marketplace
/plugin install rust-skills@rust-skills
Quick InstallInstall with npx
npx skills add ZhangHanDong/rust-skills
git cloneClone repository
git clone https://github.com/ZhangHanDong/rust-skills.git
πŸ“– Extracted from docs: goooice/rust-skills
4Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

"Use when designing resource lifecycles. Keywords: RAII, Drop, resource lifecycle, connection pool, lazy initialization, connection pool design, resource cleanup patterns, cleanup, scope, OnceCell, Lazy, once_cell, OnceLock, transaction, session management, when is Drop called, cleanup on error, guard pattern, scope guard, θ΅„ζΊη”Ÿε‘½ε‘¨ζœŸ, 连ζŽ₯ζ± , ζƒ°ζ€§εˆε§‹εŒ–, 衄源清理, RAII 樑式"

Overview

# 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:

  1. What's the resource cost?

- Cheap β†’ create per use

- Expensive β†’ pool or cache

- Global β†’ lazy singleton

  1. What's the scope?

- Function-local β†’ stack allocation

- Request-scoped β†’ passed or extracted

- Application-wide β†’ static or Arc

  1. 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):

```

"How should I manage database connections?"

↑ Ask: What's the connection cost?

↑ Check: domain-* (latency requirements)

↑ Check: Infrastructure (connection limits)

```

| Question | Trace To | Ask |

|----------|----------|-----|

| Connection pooling | domain-* | What's acceptable latency? |

| Resource limits | domain-* | What are infra constraints? |

| Transaction scope | domain-* | What must be atomic? |

---

Trace Down ↓

To implementation (Layer 1):

```

"Need automatic cleanup"

↓ m02-resource: Implement Drop

↓ m01-ownership: Clear owner for cleanup

"Need lazy initialization"

↓ m03-mutability: OnceLock for thread-safe

↓ m07-concurrency: LazyLock for sync

"Need connection pool"

↓ m07-concurrency: Thread-safe pool

↓ m02-resource: Arc for sharing

```

---

Quick Reference

| Pattern | Type | Use Case |

|---------|------|----------|

| RAII | Drop trait | Auto cleanup on scope exit |

| Lazy Init | OnceLock, LazyLock | Deferred initialization |

| Pool | r2d2, deadpool | Connection reuse |

| Guard | MutexGuard | Scoped lock release |

| Scope | Custom struct | Transaction boundaries |

Lifecycle Events

| Event | Rust Mechanism |

|-------|----------------|

| Creation | new(), Default |

| Lazy Init | OnceLock::get_or_init |

| Usage | &self, &mut self |

| Cleanup | Drop::drop() |

Pattern Templates

RAII Guard

```rust

struct FileGuard {

path: PathBuf,

_handle: File,

}

impl Drop for FileGuard {

fn drop(&mut self) {

// Cleanup: remove temp file

let _ = std::fs::remove_file(&self.path);

}

}

```

Lazy Singleton

```rust

use std::sync::OnceLock;

static CONFIG: OnceLock = OnceLock::new();

fn get_config() -> &'static Config {

CONFIG.get_or_init(|| {

Config::load().expect("config required")

})

}

```

---

Common Errors

| Error | Cause | Fix |

|-------|-------|-----|

| Resource leak | Forgot Drop | Implement Drop or RAII wrapper |

| Double free | Manual memory | Let Rust handle |

| Use after drop | Dangling reference | Check lifetimes |

| E0509 move out of Drop | Moving owned field | Option::take() |

| Pool exhaustion | Not returned | Ensure Drop returns |

---

Anti-Patterns

| Anti-Pattern | Why Bad | Better |

|--------------|---------|--------|

| Manual cleanup | Easy to forget | RAII/Drop |

| lazy_static! | External dep | std::sync::OnceLock |

| Global mutable state | Thread unsafety | OnceLock or proper sync |

| Forget to close | Resource leak | Drop impl |

---

Related Skills

| When | See |

|------|-----|

| Smart pointers | m02-resource |

| Thread-safe init | m07-concurrency |

| Domain scopes | m09-domain |

| Error in cleanup | m06-error-handling |

More from this repository10

🎯
m10-performance🎯Skill

Optimizes code performance by identifying bottlenecks, measuring impact, and guiding strategic improvements across algorithm, data structure, and memory efficiency.

🎯
m14-mental-model🎯Skill

Applies the M14 mental model framework to enhance decision-making and strategic thinking through structured cognitive analysis.

🎯
m04-zero-cost🎯Skill

Guides developers in choosing zero-cost abstractions by analyzing type system constraints and performance trade-offs in Rust generics and traits.

🎯
meta-cognition-parallel🎯Skill

Performs parallel three-layer meta-cognitive analysis by forking subagents to simultaneously analyze language mechanics, design choices, and domain constraints, then synthesizing results.

🎯
unsafe-checker🎯Skill

Identifies and reviews unsafe Rust code patterns, FFI risks, and potential memory unsafety in Rust projects.

🎯
rust-skill-creator🎯Skill

Dynamically generates Claude skills for Rust crates, standard library modules, and documentation by extracting and processing technical details from specified URLs.

🎯
coding-guidelines🎯Skill

Provides comprehensive Rust coding guidelines covering naming conventions, best practices, error handling, memory management, concurrency, and code style recommendations.

🎯
rust-refactor-helper🎯Skill

Performs safe Rust refactoring by analyzing symbol references, checking conflicts, and applying changes across project files using LSP.

🎯
m03-mutability🎯Skill

Diagnoses and guides resolution of Rust mutability and borrowing conflicts by analyzing ownership, mutation patterns, and thread-safety requirements.

🎯
m05-type-driven🎯Skill

Explores and demonstrates type-driven development techniques in Rust, showcasing advanced type system features and pattern matching strategies.