{
  "id": "rust/e0502-cannot-borrow-as-immutable-in-match-guard",
  "signature": "error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable in match guard",
  "signature_zh": "错误[E0502]：无法将 `x` 作为不可变借用，因为它在 match 守卫中也被作为可变借用",
  "regex": "error\\[E0502\\]: cannot borrow `.*` as immutable because it is also borrowed as mutable in match guard",
  "domain": "rust",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Match guards create a mutable borrow that conflicts with an immutable borrow in the same scope, violating Rust's borrowing rules.",
  "root_cause_type": "generic",
  "root_cause_zh": "match 守卫创建了一个可变借用，与同一作用域中的不可变借用冲突，违反了 Rust 的借用规则。",
  "versions": [
    {
      "version": "rustc 1.60.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.67.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.73.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The mutable borrow in the guard still conflicts because the immutable reference x_ref is still alive in the match scope.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "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.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The mutable borrow may be necessary for the match arm's logic. Removing it changes program semantics.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Restructure the match to avoid mutable borrow in guard: move the mutable operation inside the arm body using `if let` or separate match.",
      "success_rate": 0.8,
      "how": "Restructure the match to avoid mutable borrow in guard: move the mutable operation inside the arm body using `if let` or separate match.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `Cell` or `RefCell` for interior mutability: `let x = Cell::new(5); match x.get() { ... }`",
      "success_rate": 0.75,
      "how": "Use `Cell` or `RefCell` for interior mutability: `let x = Cell::new(5); match x.get() { ... }`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Extract the guard logic into a separate function that takes immutable references.",
      "success_rate": 0.85,
      "how": "Extract the guard logic into a separate function that takes immutable references.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "重构 match 以避免在守卫中进行可变借用：将可变操作移到 arm 主体内，使用 `if let` 或单独的 match。",
    "使用 `Cell` 或 `RefCell` 实现内部可变性：`let x = Cell::new(5); match x.get() { ... }`",
    "将守卫逻辑提取到接受不可变引用的单独函数中。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://doc.rust-lang.org/error_codes/E0502.html",
  "official_doc_section": null,
  "error_code": "E0502",
  "verification_tier": "ai_generated",
  "confidence": 0.83,
  "fix_success_rate": 0.78,
  "resolvable": "partial",
  "first_seen": "2023-04-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}