{
  "id": "rust/e0308-expected-usize-found-u32",
  "signature": "error[E0308]: mismatched types: expected `usize`, found `u32`",
  "signature_zh": "错误[E0308]：类型不匹配：期望 `usize`，发现 `u32`",
  "regex": "error\\[E0308\\]: mismatched types: expected `usize`, found `u32`",
  "domain": "rust",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Using a fixed-size integer (like u32) where a usize is required, typically in array indexing or slice operations.",
  "root_cause_type": "generic",
  "root_cause_zh": "在需要 usize 的地方（如数组索引或切片操作）使用了固定大小整数（如 u32）。",
  "versions": [
    {
      "version": "1.65",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.70",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.75",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Casting with `as`: `index as usize` without checking for overflow on 32-bit platforms",
      "why_fails": "This works syntactically but may cause silent truncation on 32-bit systems if the u32 value exceeds 2^32.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Changing the variable type to usize everywhere",
      "why_fails": "May break other code that expects u32, and does not address the root cause of type mismatch.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Convert explicitly with `try_into()`: `let idx: usize = index.try_into().unwrap();`",
      "success_rate": 0.95,
      "how": "Convert explicitly with `try_into()`: `let idx: usize = index.try_into().unwrap();`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `as` conversion if the value is guaranteed to fit: `let idx = index as usize;`",
      "success_rate": 0.85,
      "how": "Use `as` conversion if the value is guaranteed to fit: `let idx = index as usize;`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Change the function signature to accept usize: `fn process(index: usize)`",
      "success_rate": 0.9,
      "how": "Change the function signature to accept usize: `fn process(index: usize)`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Convert explicitly with `try_into()`: `let idx: usize = index.try_into().unwrap();`",
    "Use `as` conversion if the value is guaranteed to fit: `let idx = index as usize;`",
    "Change the function signature to accept usize: `fn process(index: usize)`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://doc.rust-lang.org/error-index.html#E0308",
  "official_doc_section": null,
  "error_code": "E0308",
  "verification_tier": "ai_generated",
  "confidence": 0.83,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-03-01",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}