{
  "id": "rust/e0515-cannot-return-value-referencing-local-variable",
  "signature": "error[E0515]: cannot return value referencing local variable `x`",
  "signature_zh": "错误[E0515]: 无法返回引用局部变量 `x` 的值",
  "regex": "error\\[E0515\\]: cannot return value referencing local variable `[^`]+`",
  "domain": "rust",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Returning a reference to a local variable that will be dropped when the function exits, creating a dangling pointer.",
  "root_cause_type": "generic",
  "root_cause_zh": "返回了对局部变量的引用，该变量在函数退出时会被释放，导致悬垂指针。",
  "versions": [
    {
      "version": "rustc 1.75.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.78.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.80.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Box::leak converts a Box to a 'static reference, but the local variable is still on the stack and will be dropped. The reference becomes dangling.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Transmuting lifetimes doesn't change the fact that the local variable is dropped. The reference becomes invalid immediately upon return.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Raw pointers don't have lifetime checks, but the data is still deallocated when the function returns, creating a dangling pointer.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Return the owned value instead of a reference. Example: fn create() -> String { let s = String::from(\"hello\"); s }",
      "success_rate": 0.95,
      "how": "Return the owned value instead of a reference. Example: fn create() -> String { let s = String::from(\"hello\"); s }",
      "condition": "",
      "sources": []
    },
    {
      "action": "Accept a reference parameter and write into it instead of returning a reference. Example: fn fill(buf: &mut String) { buf.push_str(\"hello\"); }",
      "success_rate": 0.9,
      "how": "Accept a reference parameter and write into it instead of returning a reference. Example: fn fill(buf: &mut String) { buf.push_str(\"hello\"); }",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use an arena allocator or a reference-counted pointer like Rc<T> to extend the lifetime. Example: Rc::new(local_var)",
      "success_rate": 0.85,
      "how": "Use an arena allocator or a reference-counted pointer like Rc<T> to extend the lifetime. Example: Rc::new(local_var)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Return the owned value instead of a reference. Example: fn create() -> String { let s = String::from(\"hello\"); s }",
    "Accept a reference parameter and write into it instead of returning a reference. Example: fn fill(buf: &mut String) { buf.push_str(\"hello\"); }",
    "Use an arena allocator or a reference-counted pointer like Rc<T> to extend the lifetime. Example: Rc::new(local_var)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://doc.rust-lang.org/error_codes/E0515.html",
  "official_doc_section": null,
  "error_code": "E0515",
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2024-01-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}