Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
zhanghandong avatar

M12 Lifecycle

  • 780 installs
  • 1.3k repo stars
  • Updated May 24, 2026
  • zhanghandong/rust-skills

This is a copy of m12-lifecycle by actionbook - installs and ranking accrue to the original listing.

m12-lifecycle is a Rust design agent skill that systematically decides when resources are created, used, and cleaned up for developers implementing RAII, Drop, lazy initialization, connection pools, and guard patterns.

About

m12-lifecycle is Layer 2 Design Choices in zhanghandong/rust-skills for Rust resource lifecycle decisions. The skill poses the core question—when should a resource be created, used, and cleaned up—and walks through scope, cleanup ownership, and error-path behavior before implementation. Coverage includes RAII and Drop, lazy initialization with OnceCell, Lazy, once_cell, and OnceLock, connection pool design, scope guards, transaction and session management, and cleanup-on-error patterns. Developers reach for m12-lifecycle when designing backend Rust services where incorrect lifecycle choices cause leaks, double-free risks, or pool exhaustion. The skill is user-invocable false, intended for agent-triggered design guidance via keywords like RAII, connection pool, and guard pattern.

  • 5 core lifecycle patterns with implementation guidance
  • Decision matrix for resource cost, scope, and error handling
  • RAII via Drop trait for automatic cleanup
  • Lazy initialization using OnceLock and LazyLock
  • Connection pool and scope guard pattern examples

M12 Lifecycle by the numbers

  • 780 all-time installs (skills.sh)
  • +7 installs in the week ending Jul 28, 2026 (Skillselion tracking)
  • Security screen: MEDIUM risk (skills.sh audit)
  • Data as of Jul 28, 2026 (Skillselion catalog sync)
npx skills add https://github.com/zhanghandong/rust-skills --skill m12-lifecycle

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs780
repo stars1.3k
Security audit3 / 3 scanners passed
Last updatedMay 24, 2026
Repositoryzhanghandong/rust-skills

How do you design Rust resource lifecycle and cleanup?

Systematically decide when Rust resources should be created, used, and cleaned up using RAII, lazy initialization, pools, and guards.

Who is it for?

Rust backend developers designing connection pools, sessions, or lazy-init resources who need RAII and Drop decisions before implementation.

Skip if: Beginners learning Rust syntax or frontend WASM projects without complex owned resources, pools, or error-path cleanup requirements.

When should I use this skill?

The user mentions Rust RAII, Drop, connection pool, OnceCell, OnceLock, lazy initialization, scope guard, or resource cleanup on error.

What you get

Resource lifecycle design decisions, RAII ownership plan, pool initialization strategy, and guard-pattern cleanup approach for Rust modules.

  • Lifecycle design decisions
  • RAII and pool architecture plan

By the numbers

  • Layer 2 Design Choices module in zhanghandong/rust-skills series

Files

SKILL.mdMarkdownGitHub ↗

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

"How should I manage database connections?"
    ↑ Ask: What's the connection cost?
    ↑ Check: domain-* (latency requirements)
    ↑ Check: Infrastructure (connection limits)
QuestionTrace ToAsk
Connection poolingdomain-*What's acceptable latency?
Resource limitsdomain-*What are infra constraints?
Transaction scopedomain-*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

PatternTypeUse Case
RAIIDrop traitAuto cleanup on scope exit
Lazy InitOnceLock, LazyLockDeferred initialization
Poolr2d2, deadpoolConnection reuse
GuardMutexGuardScoped lock release
ScopeCustom structTransaction boundaries

Lifecycle Events

EventRust Mechanism
Creationnew(), Default
Lazy InitOnceLock::get_or_init
Usage&self, &mut self
CleanupDrop::drop()

Pattern Templates

RAII Guard

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

use std::sync::OnceLock;

static CONFIG: OnceLock<Config> = OnceLock::new();

fn get_config() -> &'static Config {
    CONFIG.get_or_init(|| {
        Config::load().expect("config required")
    })
}

---

Common Errors

ErrorCauseFix
Resource leakForgot DropImplement Drop or RAII wrapper
Double freeManual memoryLet Rust handle
Use after dropDangling referenceCheck lifetimes
E0509 move out of DropMoving owned fieldOption::take()
Pool exhaustionNot returnedEnsure Drop returns

---

Anti-Patterns

Anti-PatternWhy BadBetter
Manual cleanupEasy to forgetRAII/Drop
lazy_static!External depstd::sync::OnceLock
Global mutable stateThread unsafetyOnceLock or proper sync
Forget to closeResource leakDrop impl

---

Related Skills

WhenSee
Smart pointersm02-resource
Thread-safe initm07-concurrency
Domain scopesm09-domain
Error in cleanupm06-error-handling

Related skills

FAQ

What question does m12-lifecycle answer?

m12-lifecycle answers when a Rust resource should be created, used, and cleaned up. The skill evaluates scope, cleanup ownership, and error-path behavior before choosing RAII, pools, or lazy initialization patterns.

Which Rust patterns does m12-lifecycle cover?

m12-lifecycle covers RAII and Drop, lazy initialization with OnceCell, Lazy, once_cell, and OnceLock, connection pool design, scope guards, and cleanup-on-error patterns for transactions and sessions.

Is M12 Lifecycle safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

Backend & APIsbackendintegrations

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.