plugins/languages/rust/skills/memory/SKILL.md
Rust 内存管理与所有权规范 — 借用规则、生命周期标注、智能指针(Box / Rc / Arc / Cow)、内部可变性(RefCell / Mutex / RwLock / atomic)、零拷贝(bytes / Cow / &[u8])、内存布局(repr / 字段排序 / SmallVec / bumpalo)。处理借用冲突、生命周期错误、内存分配优化、`Arc<Mutex<T>>` 重构时加载。触发短语:借用冲突、生命周期错误、cannot borrow、does not live long enough、智能指针、Arc Mutex、零拷贝、内存优化。
npx skillsauth add lazygophers/ccplugin rust-memoryInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
前置:rust-core 的所有权三原则。本 skill 聚焦借用、生命周期、智能指针、零拷贝。
&T 共存 OR 唯一 &mut T,二者互斥。// 缩短借用作用域
let value = map.get("k").cloned(); // 借用结束
if value.is_none() { map.insert("k", 42); }
// entry API 避免双查
map.entry("k").or_insert(42);
| 类型 | 场景 | 线程安全 | 备注 |
|------|------|---------|------|
| Box<T> | 堆分配 / 递归类型 / Box<dyn Trait> | 取决于 T | 单一所有权 |
| Rc<T> | 单线程共享只读 | 否 | 弱引用用 Weak |
| Arc<T> | 多线程共享只读 | 是 | 原子计数有开销 |
| Cow<'a, T> | 大概率只读、偶尔修改 | 取决于 T | 零或一次分配 |
// 单线程:RefCell(运行时借用检查,panic 风险)
use std::cell::RefCell;
// 多线程:Mutex / RwLock
use std::sync::{Arc, RwLock};
// 优先:原子 / channel 替代锁
use std::sync::atomic::{AtomicU64, Ordering};
let counter = AtomicU64::new(0);
counter.fetch_add(1, Ordering::Relaxed);
Arc<Mutex<T>> 是反模式信号:先考虑 mpsc channel、Arc<RwLock<T>> 或 actor 拆分。
&self 方法 → 借 self)。'a 而非 'static。'static 仅在真正跨线程 spawn 或全局数据时使用。struct Parser<'input> { input: &'input str, pos: usize }
// Cow:延迟克隆
use std::borrow::Cow;
fn normalize(p: &str) -> Cow<'_, str> {
if p.contains("//") { Cow::Owned(p.replace("//", "/")) } else { Cow::Borrowed(p) }
}
// bytes::Bytes:网络 / 缓冲零拷贝切片
use bytes::Bytes;
let head = Bytes::from_static(b"hello").slice(0..3);
// 切片视图
fn parse_header(data: &[u8]) -> &[u8] { &data[..4] }
#[repr(C)] // FFI / 显式布局
#[repr(align(64))] struct CacheAligned([u8; 64]);
// 字段按大小降序排,减少 padding
struct Optimized { a: u64, b: u32, c: u16, d: u8 }
// SmallVec:小容量栈分配
use smallvec::SmallVec;
let tags: SmallVec<[String; 4]> = SmallVec::new();
// bumpalo:批量同生命周期对象
use bumpalo::Bump;
let arena = Bump::new();
let n = arena.alloc(42);
| 参数 | 写法 |
|------|------|
| 字符串只读 | &str 或 impl AsRef<str> |
| 切片只读 | &[T] |
| 路径 | &Path / impl AsRef<Path> |
| 字节 | &[u8] / &Bytes |
| 必须拥有 | String / Vec<T> / T |
| AI 倾向 | 正确做法 |
|---------|---------|
| .clone() 绕借用 | 重构作用域 / Cow / 借用 |
| Arc<Mutex<T>> 万能 | channel / atomic / 拆分状态 |
| 'static 一把梭 | 引入 'a 缩短生命周期 |
| Vec 装小集合 | SmallVec / 数组 |
| Box<dyn Trait> | 优先泛型静态分派 |
&str、&[T]、&Path).clone() / .to_string()CowArc<Mutex<T>> 滥用rust-core:所有权三原则、错误处理rust-async:跨 await 的借用、Send 边界rust-unsafe:裸指针、手动内存development
Go 数据库规范——GORM Model 命名 ModelXxx、表名单数、枚举 uint8 + 常量、索引 idx_ 前缀 + deleted_at leading column、禁 time.Time 统一 int64 unix、禁指针/nullable 字段、TEXT/BLOB/JSON 禁 default、AutoMigrate 禁改主键。设计 DB model、写 GORM tag、建索引、做 migration 审查时触发。
development
Go HTTP API 规范——响应始终 200 + body code 字段、路由 /api/* 全 POST 单段 <Action><Model>、中间件逐路由注册禁 Group(prefix,mw...)、handler 仅返回 (rsp,error)、认证走 header。设计 HTTP API、写路由/handler/中间件时触发。
development
Go 项目结构规范——三层架构(API → Impl → State)、全局状态模式、internal/ 私有包、cmd/ 仅 main.go、go.work 多模块、禁止 Repository 接口和 DI 容器、struct 公共字段开头全 omitempty、handler var rsp 顶声明、禁 legacy migration。设计项目骨架、新建目录、组织包、做架构评审时触发。
development
Go 命名规范——Id/Uid 字段(非 ID)、IsActive/HasMFA 布尔前缀、CreatedAt 时间字段、接收者统一用 p、包名全小写无下划线、泛型类型参数描述性命名、集合字段 xxx_list 禁 xxxs 复数、Enum 0 值 XxxNil 禁 Unknown、禁 Status 统一 State、Set/Update 语义区分。定义结构体字段、函数、变量、包、接收者名、泛型、枚举时触发。