{
  "id": "rust/e0308-incompatible-types-in-if-and-else",
  "signature": "error[E0308]: `if` and `else` have incompatible types",
  "signature_zh": "错误[E0308]：`if` 和 `else` 分支的类型不兼容",
  "regex": "error\\[E0308\\]: `if` and `else` have incompatible types",
  "domain": "rust",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "The if and else branches of an if-else expression return different types, which Rust's type system cannot unify.",
  "root_cause_type": "generic",
  "root_cause_zh": "if-else 表达式的两个分支返回了不同的类型，Rust 的类型系统无法统一它们。",
  "versions": [
    {
      "version": "rustc 1.60.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.65.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "rustc 1.72.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Type annotations don't magically convert types. The else branch still returns a &str, not an i32.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Return makes the branch diverge (never produce a value), so the types can still mismatch if the other branch has a different type.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Box<dyn Trait> requires both types to implement the same trait, but Box::new alone doesn't create a trait object.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use explicit type conversion: `let x = if cond { 42_i32 } else { \"hello\".parse::<i32>().unwrap_or(0) };`",
      "success_rate": 0.8,
      "how": "Use explicit type conversion: `let x = if cond { 42_i32 } else { \"hello\".parse::<i32>().unwrap_or(0) };`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a trait object: `let x: Box<dyn std::fmt::Display> = if cond { Box::new(42) } else { Box::new(\"hello\") };`",
      "success_rate": 0.9,
      "how": "Use a trait object: `let x: Box<dyn std::fmt::Display> = if cond { Box::new(42) } else { Box::new(\"hello\") };`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use an enum: `enum MyEnum { Int(i32), Str(&'static str) } let x = if cond { MyEnum::Int(42) } else { MyEnum::Str(\"hello\") };`",
      "success_rate": 0.95,
      "how": "Use an enum: `enum MyEnum { Int(i32), Str(&'static str) } let x = if cond { MyEnum::Int(42) } else { MyEnum::Str(\"hello\") };`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "使用显式类型转换：`let x = if cond { 42_i32 } else { \"hello\".parse::<i32>().unwrap_or(0) };`",
    "使用特征对象：`let x: Box<dyn std::fmt::Display> = if cond { Box::new(42) } else { Box::new(\"hello\") };`",
    "使用枚举：`enum MyEnum { Int(i32), Str(&'static str) } let x = if cond { MyEnum::Int(42) } else { MyEnum::Str(\"hello\") };`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://doc.rust-lang.org/error_codes/E0308.html",
  "official_doc_section": null,
  "error_code": "E0308",
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2023-03-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}