# error[E0308]: `if` and `else` have incompatible types

- **ID:** `rust/e0308-incompatible-types-in-if-and-else`
- **Domain:** rust
- **Category:** type_error
- **Error Code:** `E0308`
- **Verification:** ai_generated
- **Fix Rate:** 92%

## Root Cause

The if and else branches of an if-else expression return different types, which Rust's type system cannot unify.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| rustc 1.60.0 | active | — | — |
| rustc 1.65.0 | active | — | — |
| rustc 1.72.0 | active | — | — |

## Workarounds

1. **Use explicit type conversion: `let x = if cond { 42_i32 } else { "hello".parse::<i32>().unwrap_or(0) };`** (80% success)
   ```
   Use explicit type conversion: `let x = if cond { 42_i32 } else { "hello".parse::<i32>().unwrap_or(0) };`
   ```
2. **Use a trait object: `let x: Box<dyn std::fmt::Display> = if cond { Box::new(42) } else { Box::new("hello") };`** (90% success)
   ```
   Use a trait object: `let x: Box<dyn std::fmt::Display> = if cond { Box::new(42) } else { Box::new("hello") };`
   ```
3. **Use an enum: `enum MyEnum { Int(i32), Str(&'static str) } let x = if cond { MyEnum::Int(42) } else { MyEnum::Str("hello") };`** (95% success)
   ```
   Use an enum: `enum MyEnum { Int(i32), Str(&'static str) } let x = if cond { MyEnum::Int(42) } else { MyEnum::Str("hello") };`
   ```

## Dead Ends

- **** — Type annotations don't magically convert types. The else branch still returns a &str, not an i32. (95% fail)
- **** — Return makes the branch diverge (never produce a value), so the types can still mismatch if the other branch has a different type. (75% fail)
- **** — Box<dyn Trait> requires both types to implement the same trait, but Box::new alone doesn't create a trait object. (85% fail)
