Back to list
huiali

rust-mutability

by huiali

An AI expert capability layer for Rust engineering practices, centered on modular skill orchestration and collaborative execution chains. It turns Rust’s core knowledge structures into callable reasoning and decision units, enabling diagnosis, design, and optimization in complex real-world scenarios.

1🍴 1📅 Jan 25, 2026

SKILL.md


name: rust-mutability description: "可变性专家。处理 E0596, E0499, E0502, interior mutability, Cell, RefCell, Mutex, RwLock, borrow conflict, 可变性, 内部可变性, 借用冲突" globs: ["**/*.rs"]

可变性

核心问题

数据需要变化吗?谁来控制变化?

可变性是程序状态改变的方式,需要谨慎设计。


可变性类型

类型控制者线程安全使用场景
&mut T外部调用者Yes标准可变借用
Cell<T>内部NoCopy 类型的内部可变性
RefCell<T>内部No非 Copy 类型的内部可变性
Mutex<T>内部Yes多线程内部可变性
RwLock<T>内部Yes多线程读写锁

借用规则

任何时刻只能有:
├─ 多个 &T(不可变借用)
└─ 或者一个 &mut T(可变借用)

两者不能同时存在

错误码速查

错误码含义不要说要问
E0596无法获取可变引用"add mut"这个真的需要可变吗?
E0499多个可变借用冲突"split borrows"数据结构设计对吗?
E0502借用冲突"separate scopes"为什么需要同时借用?
RefCell panic运行时借用错误"use try_borrow"运行时检查合适吗?

何时用内部可变性

// 情况1:从 &self 获取 &mut T
struct Config {
    counters: RefCell<HashMap<String, u32>>,
}

impl Config {
    fn increment(&self, key: &str) {
        // 从不可变引用获取可变引用
        let mut counters = self.counters.borrow_mut();
        *counters.entry(key.to_string()).or_insert(0) += 1;
    }
}

// 情况2:Copy 类型
struct State {
    count: Cell<u32>,
}

impl State {
    fn increment(&self) {
        self.count.set(self.count.get() + 1);
    }
}

线程安全选择

// 简单计数器 → 原子类型
let counter = AtomicU64::new(0);

// 复杂数据 → Mutex 或 RwLock
let data = Mutex::new(HashMap::new());

// 读多写少 → RwLock
let data = RwLock::new(HashMap::new());

常见问题

借用冲突

// ❌ 借用冲突
let mut s = String::new();
let r1 = &s;
let r2 = &s;
let r3 = &mut s; // 冲突!

// ✅ 分开作用域
let mut s = String::new();
{
    let r1 = &s;
    // 使用 r1
}
let r3 = &mut s;
// 使用 r3

RefCell panic

// ❌ 双重可变借用
let cell = RefCell::new(vec![]);
let mut_borrow = cell.borrow_mut();
let another = cell.borrow(); // panic!

// ✅ 用 try_borrow 避免 panic
if let Ok(mut_borrow) = cell.try_borrow_mut() {
    // 安全使用
}

设计建议

  1. 变异是必要的吗?

    • 也许可以返回新值
    • 也许可以 immutable 构造
  2. 谁控制变异?

    • 外部调用者 → &mut T
    • 内部逻辑 → 内部可变性
    • 并发访问 → 同步原语
  3. 线程上下文?

    • 单线程 → Cell/RefCell
    • 多线程 → Mutex/RwLock/Atomic

向上追踪

借用冲突持续时:

E0499/E0502 (借用冲突)
    ↑ 问:数据结构设计正确吗?
    ↑ 查 rust-type-driven:应该拆分数据吗?
    ↑ 查 rust-concurrency:涉及 async 吗?

Score

Total Score

70/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

+10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon