{
  "id": "rust/e0502-borrow-mut-and-immut-in-vec",
  "signature": "error[E0502]: cannot borrow `vec` as mutable because it is also borrowed as immutable\n\n  --> src/main.rs:10:5\n   |\n 8 |     let first = &vec[0];\n   |                  ------ immutable borrow occurs here\n 9 |     vec.push(1);\n10 |     println!(\"{}\", first);\n   |     ^^^^^^^^^^^^^^^^^^^^^^^ immutable borrow later used here",
  "signature_zh": "错误[E0502]：不能将 `vec` 作为可变借用，因为它也被作为不可变借用\n\n  --> src/main.rs:10:5\n   |\n 8 |     let first = &vec[0];\n   |                  ------ 这里发生了不可变借用\n 9 |     vec.push(1);\n10 |     println!(\"{}\", first);\n   |     ^^^^^^^^^^^^^^^^^^^^^^^ 不可变借用稍后在这里使用",
  "regex": "cannot borrow `[a-zA-Z_][a-zA-Z0-9_]*` as mutable because it is also borrowed as immutable",
  "domain": "rust",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Holding an immutable reference to a `Vec` element while trying to mutate the `Vec` (e.g., push), which could invalidate the reference.",
  "root_cause_type": "generic",
  "root_cause_zh": "持有对 `Vec` 元素的不可变引用，同时尝试修改 `Vec`（例如 push），这可能会使引用失效。",
  "versions": [
    {
      "version": "rustc 1.74",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.75",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Leads to undefined behavior; the Vec may reallocate and invalidate the pointer.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Inefficient and doesn't solve the borrow conflict; still need to manage lifetimes.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Clone the element you need before mutating: `let first = vec[0].clone(); vec.push(1); println!(\"{}\", first);`",
      "success_rate": 0.9,
      "how": "Clone the element you need before mutating: `let first = vec[0].clone(); vec.push(1); println!(\"{}\", first);`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use indices instead of references: `let first_index = 0; vec.push(1); println!(\"{}\", vec[first_index]);`",
      "success_rate": 0.85,
      "how": "Use indices instead of references: `let first_index = 0; vec.push(1); println!(\"{}\", vec[first_index]);`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在修改前克隆你需要的元素：`let first = vec[0].clone(); vec.push(1); println!(\"{}\", first);`",
    "使用索引代替引用：`let first_index = 0; vec.push(1); println!(\"{}\", vec[first_index]);`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://doc.rust-lang.org/stable/error_codes/E0502.html",
  "official_doc_section": null,
  "error_code": "E0502",
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-11-01",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}