# error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable in match guard

- **ID:** `rust/e0502-cannot-borrow-as-immutable-in-match-guard`
- **Domain:** rust
- **Category:** type_error
- **Error Code:** `E0502`
- **Verification:** ai_generated
- **Fix Rate:** 78%

## Root Cause

Match guards create a mutable borrow that conflicts with an immutable borrow in the same scope, violating Rust's borrowing rules.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| rustc 1.60.0 | active | — | — |
| rustc 1.67.0 | active | — | — |
| rustc 1.73.0 | active | — | — |

## Workarounds

1. **Restructure the match to avoid mutable borrow in guard: move the mutable operation inside the arm body using `if let` or separate match.** (80% success)
   ```
   Restructure the match to avoid mutable borrow in guard: move the mutable operation inside the arm body using `if let` or separate match.
   ```
2. **Use `Cell` or `RefCell` for interior mutability: `let x = Cell::new(5); match x.get() { ... }`** (75% success)
   ```
   Use `Cell` or `RefCell` for interior mutability: `let x = Cell::new(5); match x.get() { ... }`
   ```
3. **Extract the guard logic into a separate function that takes immutable references.** (85% success)
   ```
   Extract the guard logic into a separate function that takes immutable references.
   ```

## Dead Ends

- **** — The mutable borrow in the guard still conflicts because the immutable reference x_ref is still alive in the match scope. (85% fail)
- **** — 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% fail)
- **** — The mutable borrow may be necessary for the match arm's logic. Removing it changes program semantics. (60% fail)
