# 错误[E0502]：无法将 `x` 作为不可变借用，因为它在 match 守卫中也被作为可变借用

- **ID:** `rust/e0502-cannot-borrow-as-immutable-in-match-guard`
- **领域:** rust
- **类别:** type_error
- **错误码:** `E0502`
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

match 守卫创建了一个可变借用，与同一作用域中的不可变借用冲突，违反了 Rust 的借用规则。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| rustc 1.60.0 | active | — | — |
| rustc 1.67.0 | active | — | — |
| rustc 1.73.0 | active | — | — |

## 解决方案

1. ```
   重构 match 以避免在守卫中进行可变借用：将可变操作移到 arm 主体内，使用 `if let` 或单独的 match。
   ```
2. ```
   使用 `Cell` 或 `RefCell` 实现内部可变性：`let x = Cell::new(5); match x.get() { ... }`
   ```
3. ```
   将守卫逻辑提取到接受不可变引用的单独函数中。
   ```

## 无效尝试

- **** — The mutable borrow in the guard still conflicts because the immutable reference x_ref is still alive in the match scope. (85% 失败率)
- **** — If x is a large struct or not Clone, this may not be feasible. Also, cloning doesn't solve the borrowing conflict if the original is still mutably borrowed. (70% 失败率)
- **** — The mutable borrow may be necessary for the match arm's logic. Removing it changes program semantics. (60% 失败率)
