Rust Resource

智能指针与资源管理专家。处理 Box, Rc, Arc, Weak, RefCell, Cell, interior mutability, RAII, Drop, 堆分配, 引用计数, 智能指针--- # 智能指针与资源管理 ## 核心问题 这个资源应该用什么方式管理? 选择正确的智能指针是 Rust 编程的核心决策之一。

49 stars 7 forksSoftware Development

Install

skills CLI (Claude Code, Cursor, Codex and more)
npx skills add huiali/rust-skills --skill rust-resource
Manual install
git clone --depth 1 https://github.com/huiali/rust-skills.git /tmp/rust-skills
mkdir -p ~/.claude/skills
cp -r /tmp/rust-skills/.codex/skills/rust-resource ~/.claude/skills/rust-resource

Personal skills live in ~/.claude/skills. Use .claude/skills in a project folder to share a skill with your team.

From the SKILL.md

## 选择决策树

```
需要共享数据吗?
    │
    ├─ 否 → 单 owner
    │   ├─ 需要堆分配? → Box<T>
    │   └─ 栈上即可? → 直接值类型
    │
    └─ 是 → 需要共享
          │
          ├─ 单线程?
          │   ├─ 可变? → Rc<RefCell<T>>
          │   └─ 只读? → Rc<T>
          │
          └─ 多线程?
                ├─ 可变? → Arc<Mutex<T>> 或 Arc<RwLock<T>>
                └─ 只读? → Arc<T>
```

## 智能指针对比

| 类型 | 所有权 | 线程安全 | 适用场景 |
|-----|-------|---------|---------|
| `Box<T>` | 单 owner | Yes | 堆分配、递归类型、trait object |
| `Rc<T>` | 共享 | No | 单线程共享、避免 clone |
| `Arc<T>` | 共享 | Yes | 多线程共享、只读数据 |
| `Weak<T>` | 弱引用 | - | 打破循环引用 |
| `RefCell<T>` | 单 owner | No | 运行时借用检查 |
| `Cell<T>` | 单 owner | No | Copy 类型的内部可变性 |

## 常见错误与解决方案

### Rc 循环引用泄漏

```rust
// ❌ 内存泄漏:两个 Rc 互相引用
struct Node {
    value: i32,
    next: Option<Rc<Node>>,
}

// ✅ 解决方案:使用 Weak 打破循环
struct Node {
    value: i32,
    next: Option<Weak<Node>>,
}
```

### RefCell panic

```rust
// ❌ 运行时 panic:双重可变借用
let cell = RefCell::new(vec![1, 2, 3]);
let mut_borrow = cell.borrow_mut();
let another_borrow = cell.borrow(); // panic!

// ✅ 解决方案:使用 try_borrow
if let Ok(mut_borrow) = cell.try_borrow_mut() {
    // 安全使用
}
```

### Arc 开销投诉

```rust
// ❌ 不必要的 Arc:单线程环境
let shared = Arc::new(data);

// ✅ 单线程用 Rc
let shared = Rc::new(data);

// ❌ 多线程不必要的原子操作
// 如果确定不需要跨线程共享,就不要用 Arc
```

## 内部可变性选择
Read the full skill on GitHub

Details

Skill name
rust-resource
Path
.codex/skills/rust-resource/SKILL.md
License
MIT
Repo updated
9 February 2026

FAQ

What does the Rust Resource skill do?

智能指针与资源管理专家。处理 Box, Rc, Arc, Weak, RefCell, Cell, interior mutability, RAII, Drop, 堆分配, 引用计数, 智能指针--- # 智能指针与资源管理 ## 核心问题 **这个资源应该用什么方式管理?** 选择正确的智能指针是 Rust 编程的核心决策之一。

How do I install the Rust Resource skill?

skills CLI (Claude Code, Cursor, Codex and more): npx skills add huiali/rust-skills --skill rust-resource. Skills go in ~/.claude/skills (personal) or .claude/skills (project), and Claude loads them automatically when a task matches the description.

Is Rust Resource popular?

It lives in huiali/rust-skills, which has 49 GitHub stars and 7 forks.

Related skills

Rust Actor

49

huiali/rust-skills

Actor model expert covering message passing, state isolation, supervision trees, deadlock prevention, fault tolerance, Actix framework, and Erlang-style concurrency patterns.

Software Development

Rust Anti Pattern

49

huiali/rust-skills

Rust anti-patterns and common mistakes expert. Handles code review issues with clone abuse, unwrap in production, String misuse, index loops, and refactoring guidance.

Software Development

Rust Async

49

huiali/rust-skills

Advanced async patterns expert. Handles Stream processing, backpressure control, select/join operations, cancellation, Future trait implementation, and async runtime optimization.

Software Development

Rust Skill

49

huiali/rust-skills

Rust 编程专家技能。处理所有 Rust 相关问题:编译错误、所有权、生命周期、并发、async/await、性能优化等。触发词:Rust, cargo, 编译错误, ownership, borrow, lifetime, async, tokio, Send, Sync, Result, Error--- # Rust Expert Skill 我会像经验丰富的 Rust 开发者一样思考和解决问题。 ## 我的思维方式 ### 1. 安全第一 Rust 的类型系统是我的安全网。每一个 `&` 和 `mut` 都有其意义。 ### 2. 零成本抽象 高级代码不应该有运行时开销。如果有疑问,性能测试会说话。 ### 3. 所有权驱动设计 谁拥有这个数据?" 是我最先问的问题。 ### 4. 编译时检查优于运行时检查 尽可能在编译期发现问题,而不是依赖运行时检查。

Software Development

Rust Pin

49

huiali/rust-skills

Pin and self-referential types expert covering Pin, Unpin, Future, async state machines, pinning projection, and memory stability guarantees.

Software Development

Rust Skill Index

49

huiali/rust-skills

Rust 技能索引。提供所有技能的快速导航和查询入口。触发词:skill, index, 技能, 索引, 目录--- # Rust 技能索引 > **当前共 38 个技能**,完整内容请参见根目录 `SKILL.md`

Software Development

Browse the most popular Claude skills, all software development skills, or write your own with the skills generator.